Advanced Router
Advanced Router
The advanced router supports nested controllers in subdirectories, enabling a more organized structure. Controllers are mapped to URLs based on their directory structure.
Tips
The advanced router is ideal for applications with a complex controller hierarchy, have a large number of controllers, require a more organized structure, or require the use of route aliases.
URL Structure
A typical route format is:
/subdir/subdir/controller/action/arg1/arg2/argxHere, controller is the name of the controller class stored in the app/controllers/subdir/subdir/controller.php file, and action is the public method on the controller that will receive the request.
Info
The advanced router has a smart controller loader that automatically loads the controller based on the URL by finding the most appropriate controller in the directory structure and can be multiple levels deep.
Note
The advanced router does not support custom routes. For more advanced routing, consider the attribute or file router.
Aliases
The advanced router supports aliases, allowing you to define custom URLs for controllers. Aliases are defined in the aliases section of the app.php file:
<?php
return [
'development' => [
'router' => [
'type' => 'advanced',
'aliases' => [
'product' => 'product/get',
],
],
],
];Here, the /product URL is aliased to /product/get. Once the URL has been translated to its aliased form, the advanced router will then attempt to load the controller and execute the action as normal.
Tips
Aliases can be used to inject additional arguments into the URL, such as the alias /example which can point to /product/get/1234. The advanced router will then load the Product controller and execute the get method with 1234 as an argument.
Automatic Type Conversion
The advanced router supports the same automatic type conversion capabilities as the basic router.