Routing
In the Magento system, a URL has the following format:
<area front name>/<VendorName>/<ModuleName>/<controller name>/<action name>
<area front name>
indicates it is at the “front” of the URL. (The area name is used internally to refer to the area in configuration files. Magento provides areas such as frontend
for the storefront and adminhtml
for the administration area.)
To assign a URL to a corresponding controller and action, use the router class.
Router has an algorithm to find a matching controller, determined by request.
Then, according to a route rule, controller is assigned to URL. Use the routes.xml
file to review or change the route rules.
Routers
The routers information for the modules is described in the routerList
parameter of Magento\Framework\App\RouterList type in your di.xml
.
Each area has its own set of the routers. The Magento\Framework\App\RouterList
model is injected into FrontController.
You might need to customize the routers to change either the standard logic of processing the requests or the native Magento routers (such as, CMS router, default router, and so on). However, you must not customize the routers that are used in Magento core modules.
Routes
Configurations of the routes are stored in routes.xml
in the scopes area.
Only the standard frontend and backend routers use routes. Typically, the configuration for a route is in the following format:
%routeId%
must be at least three characters in length and can consist of the following characters: A-Z, a-z, 0-9, _
.
%frontName%
must be at least three characters in length and can consist of the following characters: A-Z, a-z, 0-9, _, -
.
To retrieve the configuration for route for an area by the specified router, use the Magento\App\Framework\Route\Config
.
To replace the controller action in a route with custom one, add the custom controller class before the original controller.
The custom controller and action should share the same names with the original ones.
The system processes the custom controller before the original, while a route remains the same.
If you must reset a route and design, forward the request processing to another route:
$this->_forward('other/controller/action')
To remove the controller action, forward to noroute
, for instance, in app/code/Company/SomeExtension/Controller/Account/Create.php
:
namespace Company\SomeExtension\Controller\Account;
class Create extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$this->_forward('noroute');
}
}
Routing processing
Routing is processed in the following way:
- Modules provide information on their routers through the
routerList
parameter ofMagento\Framework\App\RouterList
type indi.xml
. FrontController
obtains active routers and checks whether a request can be processed.- If a request cannot be processed by any router, the default router is used.
- If a request can be processed by a router, the router finds a route with matching
frontName
and looks through corresponding modules. If a module has matching controller and action names, a router instantiates this controller.
The dispatch()
method of the Magento\Framework\App\Action\Action
class requests an instance and returns its response.
For this class, the Magento\Framework\App\ActionInterface
processes the requests through its actions. Also, the following classes participate in processing the requests:
- The Magento\Framework\App\State class provides information on the state of the application, that is, current mode, installation date, and so on.
- The Magento\Framework\App\Arealist class serves to configure the application areas through the
di.xml
file - The Magento\App\Area\FrontNameResolverInterface class resolves the dynamic area’s front names
Default router
If a request cannot be processed by any router, the Magento\Framework\App\Router\DefaultRouter default router lists handlers for processing such request.
Magento\App\Router\NoRouteHandlerList contains the list of handlers.
Related information
See The Route Config Kata by Magento contributor Vinai Kopp.