2013-12-16 24 views
1

我試圖理解Laravel 4中的路由。我讀了一篇很好的post here on StackOverflow和一篇關於手動指定路由的文章beware the route to evil的鏈接。我喜歡手動指定我的路線並使routes.php充當文檔的想法。但似乎我需要謹慎對待我的路線的順序,如果我要指定我自己的而不是使用Route::resource()如果我有newcreate路線在show之前,那麼我不會路由到該節目,因爲URI中的變量?路線的定義順序很重要?在Laravel 4中指定安靜路線的必需順序?

// This will not work if I try and browse to dogs/new 
Route::get('dogs', array('as' => 'dogs', 'uses' => '[email protected]')); 
Route::get('dogs/{dogs}', array('as' => 'dog', 'uses' => '[email protected]')); 
Route::get('dogs/new', array('as' => 'new_dog', 'uses' => '[email protected]')); 

看來我需要確保的是,dogs/new而來的dogs/{dogs}新前正確地返回。我不清楚什麼{dogs}做什麼或不同於(:any){any}我在示例和僞代碼中看到了幾種不同的用法。我看到/new{...}相同,當時路由是在Laravel 4中的通配符之前更具體的{}。 (...)是舊的方式嗎?

順便說一句,我從一些時,我與像Route::resource('photos', 'PhotosController');方法的資源路線行駛php artisan routes我見過的例子注意到一個不同的命名約定並命名爲後路線索引來創建新的資源命名爲photos.store和@store。鏈接到表單以創建新資源的方法和命名路由爲photos.create和@create。 Laravel 4是其他框架中的東西還是約定?

回答

0
Route::get('dogs/{dogs}', array('as' => 'dog', 'uses' => '[email protected]')); 

上面的url期待狗段後的參數。例如: 例如:http://laravel.com/dogs/xyz,http://laravel.com/dogs/new

狗狗url段後,Laravel會接受任何東西。因此,您的另一個路由將永遠不會執行路由參數。

Route::get('dogs/new', array('as' => 'new_dog', 'uses' => '[email protected]')); 

更多關於路由參數:

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

資源控制器 LaravelRuby on rails支持資源全路由。我認爲,裁縫借用Ruby中的資源完全路由思想。

以下航線將產生,如果你使用的資源控制器:

​​

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

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