Back to Top

Magento 2 Routing

Updated 13 August 2024

Magento 2 routing is one of the most important parts. Complete application (Magento 2) flow depends on processing URL requests and router classes which are responsible for matching and processing those requests.

It enhances overall security and provides a clear understanding of the flow.

In Magento 2 a standard URL or a route contains three parts,
i) Front Name
ii) Controller Name
iii) Action Name

We need to register the front name with the routes.xml file which will be under the etc/frontend folder. Here frontend is an area code that refers to the front-end area. If we were creating a route for admin then we have to use adminhtml as the area code.

Selection_083
File Structure

Code for etc/frontend/routes.xml file

Searching for an experienced
Magento 2 Company ?
Find out More
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="blogmanager" frontName="blog">
            <module name="Webkul_BlogManager" />
        </route>
    </router>
</config>

Here the id is blogmanager and frontName is blog. Often for simplicity, we use the same value for id and frontName but this is not a requirement.

Here the id specifies the unique node id for this route in Magento and is also the first segment of its associated layout handle XML filename (routeId_controller_action.xml).

Multiple modules can share the same route so we need to specify our module in the route tag.

For more clarification on what if id and frontName are different in routes.xml in Magento you can refer Magento 2 blog.

We need to create a folder with the name Controller inside our module folder (ModuleName/Controller) which will contain all controller-related folders and files.

Selection_084
Folder Structure

Now we need to create a folder with our controller name (eg. Manage) like (Controller/Manage) and under the Manage folder we need to create the action file Add.php

Selection_085-1
Folder Structure

Code for Controller/Manage/Add.php file

<?php
namespace Webkul\BlogManager\Controller\Manage;

use Magento\Customer\Controller\AbstractAccount;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Add extends AbstractAccount
{
    /**
     * @var PageFactory
     */
    private $resultPageFactory;

    /**
     * Dependency Initilization
     *
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
	PageFactory $resultPageFactory
    ) {
	$this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Provides content
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
      echo('<h1>Hello World</h1>');
    }
}

The namespace should follow the directory structure and the class name will be filename.

Our action is extending Magento\Customer\Controller\AbstractAccount so this action can be accessed by logged-in customers only. If an action extends Magento\Framework\App\Action\Action then that can be accessed by the guest user also.

When we access the URL, the execute function will be called. The URL is given by frontName/controllerName/actionName which is blog/manage/add in our case.

2021-01-27_21-07

Note that since we are extending Magento\Customer\Controller\AbstractAccount class, we need to login first to access otherwise it will redirect to the login page. If you face any error then run the di compile command to compile the code.

php bin/magento setup:di:compile

Folder structure till now,

Selection_086

Next Blog -> Magento 2 Create Tables

Previous Blog-> Magento 2 Module Registration

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home