以此控制器为例:
<?php
class TestController extends BaseController {
public function getIndex()
{
echo "a";
}
public function postSecond($a)
{
echo "b";
}
}
In your routes, if you have
Route::controller('tests', 'TestController');
And execute
php artisan routes
您将拥有:
+--------+--------------------------------------------+------------------------+-----------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+--------------------------------------------+------------------------+-----------------------------------+----------------+---------------+
| | GET /tests/index/{v1}/{v2}/{v3}/{v4}/{v5} | | TestController@getIndex | | |
| | GET /tests | | TestController@getIndex | | |
| | POST /tests | tests.store | TestController@store | | |
| | GET /tests/{_missing} | | TestController@missingMethod | | |
+--------+--------------------------------------------+------------------------+-----------------------------------+----------------+---------------+
Laravel inspects the controller and generates routes based on what methods it finds, automatically.
但如果你这么做了
Route::resource('tests', 'TestController');
You'll get this route listing:
+--------+--------------------------------------------+------------------------+-----------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+--------------------------------------------+------------------------+-----------------------------------+----------------+---------------+
| | GET /tests | | Closure | | |
| | GET /tests | tests.index | TestController@index | | |
| | GET /tests/create | tests.create | TestController@create | | |
| | POST /tests | tests.store | TestController@store | | |
| | GET /tests/{tests} | tests.show | TestController@show | | |
| | GET /tests/{tests}/edit | tests.edit | TestController@edit | | |
| | PUT /tests/{tests} | tests.update | TestController@update | | |
| | PATCH /tests/{tests} | | TestController@update | | |
| | DELETE /tests/{tests} | tests.destroy | TestController@destroy | | |
+--------+--------------------------------------------+------------------------+-----------------------------------+----------------+---------------+
不用猜测,Laravel使用预定义的CRUD路由列表,您可以删除其中一些路由,但它不会判断控制器以为您的方法构建路由.
You decide what's best for you. But, usually, if your controller is a CRUD one, Route::resource() is a good start, otherwhise you can use Route::controller() or build your routes manually.
编辑:
没有真正为什么一个或为什么另一个,只是一个设计和 Select 的问题.有些人永远不会使用它们中的任何一个.只是HAT Route::resource()
遵循Rails的路由方式:http://guides.rubyonrails.org/routing.html.
使用Route::resource()
,您不需要创建所有这些方法,但最终会得到一个毫无意义的路由列表,因为默认情况下,Laravel总是创建所有这些路由,除非您:
Route::resource('photo', 'PhotoController',
array('only' => array('index', 'show')));
而您的路由列表将只显示索引和显示操作.
此外,如果您需要一些其他路由,使用Route::resource()
,您将不得不手动构建它们或使用一些魔术,使它们自动为您所有的足智多谋的路由.使用Route::controller()
一切都是自动的,每次您添加新方法时,都会为您创建一条新路由.
同样,如果要构建CRUD控制器,请从使用Route::resource()
开始.否则,在你的特殊情况下,考虑一下其中一个的好处.
EDIT2:
这是来自Phil Sturjo(PyroCMS和PHP-FIG)的一篇很棒的文章,它讲述了手动构建所有路由的好处:http://philsturgeon.co.uk/blog/2013/07/beware-the-route-to-evil.