简体   繁体   English

laravel 4.2路由器对象

[英]laravel 4.2 Router objects

I am working on laravel project to learn the framework, i have a question comes in my mind about routing. 我正在研究laravel项目以学习框架,我想到一个有关路由的问题。
Is the router a singleton class? 路由器是单身人士吗? because i try the following in the route.php file 因为我在route.php文件中尝试了以下内容

$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. 最后一行显示$route1对象的路由,输出为。

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. 输出显示$route1对象具有由$route2$route3$route4对象创建的其他路由。
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. 向您展示的最好方法是查看symfony路由组件。 http://symfony.com/doc/current/components/routing/introduction.html http://symfony.com/doc/current/components/routing/introduction.html

You have a route, route collection, request, and url matcher. 您有一个路由,路由集合,请求和URL匹配器。

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. 获取请求网址,然后使用匹配器将该网址与路由进行匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM