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.xmlfile located in the module’s
etcdirectory. 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
idattribute of the
routerelement in the
routerelement in the
routes.xmlfile 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.
controllerelement in the
routes.xmlfile defines the controller class that will handle the request for the specific route.
actionelement in the
routes.xmlfile 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.phpfile in the root directory of the Magento installation. The
index.phpfile is responsible for initializing the Magento application and routing the request to the appropriate controller.
- Magento 2 routes are defined in the
routes.xmlfile in each module’s
routes.xmlfile 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\RouterInterfaceinterface and defining the router in the
di.xmlfile 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\UrlFactoryclasses. 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
First, we’ll define the route in the
In 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:
In 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:
In 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: