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:

  1. The user clicks on the link /user/register, thereby sending a request.
  2. The frontend application is launched and routing is initialized.
  3. The routing component registers all the routes of modules and extensions.
  4. The received list is processed to determine if there is a match for the URL.
  5. As a result, the component returns the users module and the register method that needs to be called.
  6. The application calls the method of the frontend controller of the users module (/modules/users/users.class.php).
  7. The method generates a response in the form of a string (HTML or JSON) and sends it to the application.
  8. 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.