In Magento 2, routes are used to define how a specific URL or path is handled by the system. When a user requests a specific URL, the system uses routes to determine which controller, action, and parameters should be executed to process the request.
Here is a brief overview of how routes work in Magento 2:
- Routes are defined in the
routes.xml
file located in the module’setc
directory. This file specifies the URL paths that should be associated with a specific controller and action. - Each route has a unique name, which is defined in the
id
attribute of therouter
element in theroutes.xml
file. - The
router
element in theroutes.xml
file defines the frontName, which is the first part of the URL path that is associated with the module. For example, if the frontName is “blog”, then URLs that start with “/blog/” will be handled by the module. - The
controller
element in theroutes.xml
file defines the controller class that will handle the request for the specific route. - The
action
element in theroutes.xml
file specifies the method in the controller class that will be executed to process the request. - Additional parameters can be passed through the URL or query string, which are then passed to the controller action method for further processing.
- Magento 2 uses the Front Controller pattern, which means that all requests go through the
index.php
file in the root directory of the Magento installation. Theindex.php
file is responsible for initializing the Magento application and routing the request to the appropriate controller. - Magento 2 routes are defined in the
routes.xml
file in each module’setc
directory. Theroutes.xml
file specifies the frontName (the part of the URL that comes after the domain name) and the module that will handle the request. - Each route can have multiple actions, and each action is mapped to a controller and action method. The action name is the part of the URL that comes after the frontName. For example, in the route “mymodule/contactus”, “contactus” is the action name.
- Magento 2 provides several predefined routers, including the standard router (
Magento\Framework\App\Router\Base
), the admin router (Magento\Backend\App\Router
), and the REST router (Magento\Webapi\Controller\Rest\Router
). Each router is responsible for matching the request to the appropriate module and controller based on the URL. - You can create custom routers in Magento 2 by implementing the
Magento\Framework\App\RouterInterface
interface and defining the router in thedi.xml
file of your module. Custom routers can be used to handle requests for specific URLs or to modify the request before it is passed to the controller. - Magento 2 also provides several helper classes for working with URLs, including the
Magento\Framework\UrlInterface
andMagento\Framework\UrlFactory
classes. These classes can be used to generate URLs for specific routes or to modify existing URLs.
Let’s say we have a custom module called “MyModule” that provides a custom “Contact Us” form. To handle requests for this form, we need to define a new route in the module’s routes.xml
file.
First, we’ll define the route in the routes.xml
file:
<?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="mymodule" frontName="mymodule">
<module name="MyModule" />
</route>
</router>
</config>
XMLIn this example, we’re defining a new route with the ID “mymodule” and the frontName “mymodule”. This means that any URL that starts with “/mymodule/” will be handled by the module.
Next, we’ll define the controller that will handle the request:
namespace MyModule\Controller\Index;
class ContactUs extends \Magento\Framework\App\Action\Action
{
public function execute()
{
// code to handle the request
}
}
PHPIn this example, we’re creating a new controller class called “ContactUs” in the “Index” namespace of the “MyModule” module. This controller extends the base Magento\Framework\App\Action\Action
class and has an execute()
method that will be executed when the route is requested.
Finally, we’ll map the route to the controller and action:
<router id="standard">
<route id="mymodule" frontName="mymodule">
<module name="MyModule" />
<action name="contactus" path="contactus" >
<controller>Index\ContactUs</controller>
<title>Contact Us</title>
<resource>MyModule::contactus</resource>
<acl>MyModule::contactus</acl>
</action>
</route>
</router>
XMLIn this example, we’re mapping the “contactus” action to the “Index\ContactUs” controller. This means that when a user visits the URL “/mymodule/contactus”, the execute()
method in the ContactUs
controller will be executed.
Check the below related topic: