Laravel 4 定义 RESTful 控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18402298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 defining RESTful controllers
提问by Iain
So I'm new to the Laravel framework as of v4 and wondering what is the way to create and use RESTful controllers. Reading through the documentation, I'm a bit confused as to the difference between RESTful controllers and Resource controllers.
所以我是 v4 的 Laravel 框架的新手,想知道创建和使用 RESTful 控制器的方法是什么。通读文档,我对 RESTful 控制器和资源控制器之间的区别有点困惑。
When defining a RESTful controller, as per the docs, it suggests doing the following in routes.php
:
在定义 RESTful 控制器时,根据文档,它建议在 中执行以下操作routes.php
:
Route::controller('posts', 'PostController');
In the PostController
, do we define the class methods by prefixing the name of the method with the HTTP verb we should like to use? For example:
在 中PostController
,我们是否通过在方法名称前加上我们想要使用的 HTTP 动词来定义类方法?例如:
class PostController extends \BaseController {
public function getIndex()
{
//
}
}
However, it also outlines a way of creating Resource controllers in the routes.php
file like so:
Route::resource('posts', 'PostController');
但是,它还概述了在routes.php
文件中创建资源控制器的方法,如下所示: Route::resource('posts', 'PostController');
And in PostController.php
we define methods without prefixing it with the HTTP verb.
并且在PostController.php
我们定义的方法中没有使用 HTTP 动词作为前缀。
class PostController extends \BaseController {
public function index()
{
//
}
}
What is the difference between the two? And when do we use one instead of the other, and why?
两者有什么区别?我们什么时候使用一个而不是另一个,为什么?
Also, should we use Route::controller('posts', 'PostController');
or Route::resource('posts', 'PostController');
to pass routing to the controller or should we define each route manually, like below:
此外,我们应该使用Route::controller('posts', 'PostController');
或Route::resource('posts', 'PostController');
将路由传递给控制器,还是应该手动定义每个路由,如下所示:
Route::get('/users', 'UserController@index');
Route::get('/users/create', 'UserController@create');
Route::post('/users', 'UserController@store');
Route::get('/users/{id}', 'UserController@show');
Route::get('/users{id}/edit', 'UserController@edit');
Route::put('/users', 'UserController@update');
Route::delete('/users', 'UserController@destroy');
回答by Antonio Carlos Ribeiro
Take this controller as example:
以这个控制器为例:
<?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
You'll have:
你将拥有:
+--------+--------------------------------------------+------------------------+-----------------------------------+----------------+---------------+
| 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.
Laravel 会检查控制器并根据它找到的方法自动生成路由。
But if you do
但是如果你这样做
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 | | |
+--------+--------------------------------------------+------------------------+-----------------------------------+----------------+---------------+
No guessing, Laravel uses a predefined CRUD list of routes, you can remove some of those routes but it won't inspect your controller to build routes for your methods.
不用猜测,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.
你决定什么最适合你。但是,通常,如果您的控制器是 CRUD 控制器,Route::resource() 是一个好的开始,否则您可以使用 Route::controller() 或手动构建您的路由。
EDIT:
编辑:
There no really why one or why another, is just a matter of design and choice. Some will use none of them, ever. It's just hat Route::resource()
follows the Rails way of routing: http://guides.rubyonrails.org/routing.html.
没有真正的为什么一个或为什么另一个,只是设计和选择的问题。有些人永远不会使用它们。这只是Route::resource()
遵循 Rails 路由方式:http: //guides.rubyonrails.org/routing.html。
Using Route::resource()
you don't need to create all those methods, but you'll end up with a list of pointless routes, because Laravel always create all of them by default, unless you do:
使用Route::resource()
你不需要创建所有这些方法,但你最终会得到一个毫无意义的路由列表,因为 Laravel 总是默认创建所有这些方法,除非你这样做:
Route::resource('photo', 'PhotoController',
array('only' => array('index', 'show')));
And your list of routes will show only the index and show actions.
并且您的路线列表将仅显示索引并显示操作。
Also, if you need some other routes, using Route::resource()
you'll have to build them manually or work some magic to make them automatic for all your resourceful routes. Using Route::controller()
everything is automatic, everytime you add a new method, a new route is created for you.
此外,如果您需要一些其他路线,使用Route::resource()
您必须手动构建它们或使用一些魔法使它们自动用于您所有足智多谋的路线。使用Route::controller()
一切都是自动的,每次添加新方法时,都会为您创建一条新路线。
Again, if you have a CRUD controller to build, start by using Route::resource()
. Otherwise, think about the benefits of one or another in your particular case.
同样,如果您要构建 CRUD 控制器,请从使用Route::resource()
. 否则,请考虑在您的特定情况下一个或另一个的好处。
EDIT2:
编辑2:
This is a great article, from Phil Sturgeon (PyroCMS and PHP-FIG), about the benefits of manually build all your routes: http://philsturgeon.co.uk/blog/2013/07/beware-the-route-to-evil.
这是一篇很棒的文章,来自 Phil Sturgeon(PyroCMS 和 PHP-FIG),关于手动构建所有路由的好处:http: //philsturgeon.co.uk/blog/2013/07/beware-the-route-to -邪恶的。
回答by Andrew Koper
@Antonio's answer is good. Let me say something similar and important a little more concisely. In routes.php:
@Antonio 的回答很好。让我更简洁地说一些类似和重要的事情。在routes.php中:
Route::resource('users', 'UserController');
Using the resource method makes Laravel assume CRUD functionality and it only looks for its six, pre-made CRUD methods: index, create, store, show, destroy, etc. It won't "see" any other, new methods you create there.
使用资源方法使 Laravel 承担 CRUD 功能,它只查找它的六个预制 CRUD 方法:索引、创建、存储、显示、销毁等。它不会“看到”你在那里创建的任何其他新方法.
Route::controller('info', 'InfoController');
Using the controller method allows you to create custom methods/pages. Laravel looks for them when you prepend the method/page name with a HTTP verb. In your XxxxController.php:
使用控制器方法允许您创建自定义方法/页面。当您在方法/页面名称前加上 HTTP 动词时,Laravel 会查找它们。在你的 XxxxController.php 中:
class InfoController extends \BaseController {
public function getFeatures()
{
return View::make('info.features');
}
public function getContactUs()
{
return View::make('info.contact-us');
}
public function getPricing()
{
return View::make('info.pricing');
}
}