I am working on laravel project to learn the framework, i have a question comes in my mind about routing.
Is the router a singleton class? because i try the following in the route.php
file
$route1 = App::make('router');
$route2 = App::make('router');
$route3 = App::make('router');
$route4 = App::make('router');
$route1->get('/r1', function(){
echo "route 1";
});
$route2->get('/r2', function(){
echo "route 2";
});
$route3->get('/r3', function(){
echo "route 3";
});
$route3->get('/r4', function(){
echo "route 4";
});
var_dump($route1->getRoutes());
as you see i have create four objects of router class, each object add one route. last line prints the routes for $route1
object, and the output is.
object(Illuminate\Routing\RouteCollection)[112]
protected 'routes' =>
array (size=2)
'GET' =>
array (size=4)
'r1' =>
object(Illuminate\Routing\Route)[120]
...
'r2' =>
object(Illuminate\Routing\Route)[122]
...
'r3' =>
object(Illuminate\Routing\Route)[124]
...
'r4' =>
object(Illuminate\Routing\Route)[126]
...
The output shows that the $route1
object have the other routes created by $route2
, $route3
, and $route4
objects.
How the routs shared between them?
You have two components here a route and a route collection. When you register a route they all get added to a route collection. The best to show you is by seeing the symfony route components. http://symfony.com/doc/current/components/routing/introduction.html
You have a route, route collection, request, and url matcher.
You create routes and gather them in a route collection.
The get the request url and use the matcher to match the url with the route.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.