【问题标题】:Required order for specifying restful routes in Laravel 4?在 Laravel 4 中指定宁静路线所需的顺序?
【发布时间】:2014-01-03 08:52:01
【问题描述】:

我正在尝试理解 Laravel 4 中的路由。我阅读了一篇很好的 post here on StackOverflow 和指向 beware the route to evil 的链接,这是一篇关于手动指定路由的帖子。我喜欢手动指定我的路由并将 routes.php 作为文档的想法。但是,如果我要指定自己的路线而不是使用Route::resource(),我似乎需要谨慎对待路线的顺序如果我在show 之前有newcreate 路线,那么我赢了'不会因为 URI 中的变量而被路由到节目?定义路线的顺序很重要,对吧?

// This will not work if I try and browse to dogs/new
Route::get('dogs', array('as' => 'dogs', 'uses' => 'DogsController@index'));  
Route::get('dogs/{dogs}', array('as' => 'dog', 'uses' => 'DogsController@show'));
Route::get('dogs/new', array('as' => 'new_dog', 'uses' => 'DogsController@create')); 

看来我需要确保 dogs/new 出现在 dogs/{dogs} 之前,以便 new 正确返回。我不清楚{dogs} 的作用或与(:any){any} 的不同之处我在示例和伪代码中看到了一些不同的用途。我看到 /new{...} 相同,当路由在更具体的 {} 之前就像 Laravel 4 中的通配符? (:...) 是旧方式吗?

顺便说一句,当我使用Route::resource('photos', 'PhotosController'); 之类的资源路由运行php artisan routes 时,我注意到与一些示例不同的命名约定。新资源名为 photos.store 和 @store。用于创建新资源的表单链接的方法和命名路径是 photos.create 和 @create。那是 Laravel 4 的东西还是其他框架中的约定?

【问题讨论】:

    标签: php rest laravel-4 routing laravel-routing


    【解决方案1】:
    Route::get('dogs/{dogs}', array('as' => 'dog', 'uses' => 'DogsController@show'));
    

    上面的网址在狗段之后需要一个参数。 例如:http://laravel.com/dogs/xyzhttp://laravel.com/dogs/new

    在狗 url 段之后,Laravel 将接受任何内容。因此,您的另一个路由将永远不会为 route 参数执行。

    Route::get('dogs/new', array('as' => 'new_dog', 'uses' => 'DogsController@create'));
    

    更多关于路由参数:

    http://laravel.com/docs/routing#route-parameters

    资源控制器 LaravelRuby on rails 支持资源全路由。我认为,Tailor 借鉴了 Ruby on rails 的资源全路由思想。

    如果使用资源控制器,会生成以下路由:

    index
    create
    store
    update
    show
    edit
    destroy
    

    http://guides.rubyonrails.org/routing.html

    http://laravel.com/docs/controllers#resource-controllers

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      相关资源
      最近更新 更多