Routing
Routing provides a mechanism for determining the module or extension responsible for handling requests to the application.
For example, let's see how the "Registration" section becomes available:
- The user clicks on the link /user/register, thereby sending a request.
- The frontend application is launched and routing is initialized.
- The routing component registers all the routes of modules and extensions.
- The received list is processed to determine if there is a match for the URL.
- As a result, the component returns the users module and the register method that needs to be called.
- The application calls the method of the frontend controller of the users module (/modules/users/users.class.php).
- The method generates a response in the form of a string (HTML or JSON) and sends it to the application.
- The application sends it to the user.
Module Routes
Module routes should be declared in the routes.php file in the module directory. Here is an example of declaring a route in a file:
<?php
return [
'users-auth' => [
'pattern' => 'user/{action}',
'callback' => 'users/$1/',
'priority' => 180,
],
];
In this example, we handle URLs of the form /user/{any word} and specify the users module for them.
We also specify $1 as the method name, which will be substituted with the word from the URL {action}
.
This is the route that handles requests to the /user/register page mentioned earlier.
Extension Routes
For more information on how to declare extension routes, please refer to the "Extensions" section.