{"id":1321,"date":"2020-10-28T13:52:11","date_gmt":"2020-10-28T13:52:11","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/10\/28\/create-route-and-controller-in-magento-2\/"},"modified":"2025-04-23T15:46:52","modified_gmt":"2025-04-23T10:16:52","slug":"create-route-and-controller-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-route-and-controller-in-magento-2\/","title":{"rendered":"How to Create Route and Controller in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In Magento 2, when you execute code to see the output on the frontend, the action method is used.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To show any message on the frontend while developing Magento 2 extension, the concepts of controller, route and actions are the fundamental things you need to understand.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to display a message on the frontend, routes.xml and Index.php are important files that every Magento extension developer has to create.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>http:\/\/example.com\/route_name\/controller\/action<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let us understand the path.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A<strong>&nbsp;route_name<\/strong>&nbsp;is a unique name that you have to set in&nbsp;<strong>routes.xml<\/strong>&nbsp;file for executing the action.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The<strong>&nbsp;controller<\/strong>&nbsp;is a class that is located in the controller folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>action<\/strong>&nbsp;is an actual controller class in which there will be an execute() method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, to execute and run your first Magento 2 extension, let us create&nbsp;<strong>routes.xml<\/strong>&nbsp;file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Create Route and Controller in Magento 2:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create routes.xml in Magento 2:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>app\/code\/Meetanshi\/Extension\/etc\/frontend\/routes.xml<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create routes.xml on this path.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" ?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\">\n    &lt;router id=\"standard\">\n        &lt;route frontName=\"extension\" id=\"extension\">\n            &lt;module name=\"Meetanshi_Extension\"\/>\n        &lt;\/route>\n    &lt;\/router>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we are going forward for creating a controller file that is Index.php.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Index.php Controller in Magento 2:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>app\/code\/Meetanshi\/Extension\/Controller\/Index\/Index.php<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Utilising the path, use the code given below for creating Index.php<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Controller\\Index;\n\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\nuse Magento\\Framework\\App\\Action\\Action;\n\nclass Index extends Action\n{\n    protected $resultPageFactory;\n\n    public function __construct(Context $context, PageFactory $resultPageFactory)\n    {\n        parent::__construct($context);\n        $this->resultPageFactory = $resultPageFactory;\n    }\n\n    public function execute()\n    {\n        echo(\"Meetanshi Extension\");\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the execute() method is called when the router matches with the controller action class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After creating this file, clear the cache memory. You can also clear cache from the below command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php bin\/magento cache:clean\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, type the URL based on your domain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:&nbsp;<strong><em>http:\/\/example.com\/extension\/index\/index<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Based on the path, either you execute on the localhost or live site, the output will be displayed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By executing the module,&nbsp;<strong>Meetanshi Extension&nbsp;<\/strong>will be printed on the frontend.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-Create-Route-and-Controller-in-Magento-2.jpg\" alt=\"How to Create Route and Controller in Magento 2\" class=\"wp-image-11147\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you get the output without any error, that means you have developed controller and route.xml file properly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulation! You have got your first Magento 2 extension running successfully.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do consider sharing this blogpost with beginners who are passionate about learning Magento extension development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read more:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/create-module-in-magento-2\/\">How to Create Module in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/create-block-and-template-in-magento-2\/\">How to Create Block and Template in Magento 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In Magento 2, when you execute code to see the output on the frontend, the action method is used. To show any message on the&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1321","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1321"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1321\/revisions"}],"predecessor-version":[{"id":12695,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1321\/revisions\/12695"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}