{"id":57225,"date":"2023-04-28T14:12:24","date_gmt":"2023-04-28T08:42:24","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57225"},"modified":"2023-05-04T14:35:46","modified_gmt":"2023-05-04T09:05:46","slug":"step-by-step-guide-to-creating-a-controller-in-drupal-8-9-10","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/step-by-step-guide-to-creating-a-controller-in-drupal-8-9-10\/","title":{"rendered":"Step-by-Step Guide to Creating a Controller in Drupal 8\/9\/10"},"content":{"rendered":"<ol>\n<li>Create a custom module: If you haven&#8217;t already done so, create a custom module in Drupal 10. You can refer to the previous question for a step-by-step guide on creating a custom module.<\/li>\n<li>Define the routing file: In the module folder, create a new file with the extension &#8220;.routing.yml&#8221;. This file defines your module&#8217;s page&#8217;s URL path and corresponding controller. For example, if you want to create a controller for the path &#8220;\/ttn-hello-world&#8221;, your routing file would look like this:<\/li>\n<\/ol>\n<blockquote>\n<pre>custom_module.ttn_hello_world:\r\npath: '\/ttn-hello-world'\r\ndefaults:\r\n_controller: '\\Drupal\\custom_module\\Controller\\TTNHelloWorldController::content'\r\n_title: 'TTN Hello, World!'\r\nrequirements:\r\n_permission: 'access content'\r\n\r\nIn this example, the \"custom_module.ttn_hello_world\" route maps to the \"TTNHelloWorldController::content\" controller, which we'll define in the next step. The \"_title\" parameter sets the page title, and the \"_permission\" parameter restricts access to users with the \"access content\" permission.<\/pre>\n<\/blockquote>\n<ol start=\"3\">\n<li>Create the controller class: In the module folder, create a new file with the extension &#8220;.php&#8221; in the &#8220;src\/Controller&#8221; directory. This file defines the controller class for your page. For example, if you want to create a controller for the path &#8220;\/ttn-hello-world&#8221;, your controller class would look like this:<\/li>\n<\/ol>\n<blockquote>\n<pre>&lt;?php\r\n\r\nnamespace Drupal\\custom_module\\Controller;\r\n\r\nuse Drupal\\Core\\Controller\\ControllerBase;\r\n\r\nclass TTNHelloWorldController extends ControllerBase {\r\n\r\n\/**\r\n* Returns the page content.\r\n*\/\r\npublic function content() {\r\n$output = [\r\n'#markup' =&gt; $this-&gt;t('TTN Hello, World!'),\r\n];\r\nreturn $output;\r\n}\r\n\r\n}<\/pre>\n<\/blockquote>\n<p>Now clear cache and access page : \/ttn-hello-world<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Create a custom module: If you haven&#8217;t already done so, create a custom module in Drupal 10. You can refer to the previous question for a step-by-step guide on creating a custom module. Define the routing file: In the module folder, create a new file with the extension &#8220;.routing.yml&#8221;. This file defines your module&#8217;s page&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1368,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":226},"categories":[3602],"tags":[4862,5201],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57225"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1368"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57225"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57225\/revisions"}],"predecessor-version":[{"id":57329,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57225\/revisions\/57329"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}