Routers¶
What are Routers?¶
Routers are StandAlone-Classes in which you can define Routes and corresponding Route-Functions.
How to create a Router?¶
You can use the CLI or simply create a new class based on the Router-Skeleton.
A new Router must extend the BaseRoute
-Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php
namespace Routes;
use Orchestra\Core\Router\BaseRoute;
class YOUR_ROUTER_NAME extends BaseRoute
{
protected $routes = [
'TARGET' => [
'METHOD' ,
'NAME' ,
'ACTION'
]
];
}
|
How to register a new Route?¶
Routes can be registered in two different ways:
Register your routes within the $routes
-property on the class:
1 2 3 4 5 6 7 8 9 | <?php
protected $routes = [
'TARGET' => [
'METHOD' ,
'NAME' ,
'ACTION'
]
];
|
$routes
must be an associative Array containing a nested Array for each Route.
Register routes in the boot
-Method of the class
Every Router can implement a boot
-Method that gives access to the Route-Modifier-Functions.
Example:
1 2 3 4 5 | <?php
public function boot(){
$this->addRoute(['TARGET' => ['METHOD','NAME','ACTION']]);
}
|
How to register an action to a new Router?¶
Actions can be defined either inside the Router itself, or in a separate Controller - just as you like [1].
[1] | Which option you choose has almost no effect on the perfomance, though Router-Based Routes are executed a little faster and they spare memory since the Controller doesn’t have to be instantiated |