Laravel 4定义RESTful控制器 [英] Laravel 4 defining RESTful controllers

查看:54
本文介绍了Laravel 4定义RESTful控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,从v4开始,我还是Laravel框架的新手,我想知道创建和使用RESTful控制器的方式是什么.仔细阅读文档,我对RESTful控制器和Resource控制器之间的区别感到困惑.

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.

根据文档定义RESTful控制器时,建议在routes.php中执行以下操作:

When defining a RESTful controller, as per the docs, it suggests doing the following in routes.php:

Route::controller('posts', 'PostController');

PostController中,我们是否通过在方法名称前加上我们要使用的HTTP动词来定义类方法?例如:

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:

class PostController extends \BaseController {
    public function getIndex()
    {
        //
    }
}

但是,它也概述了在routes.php文件中创建资源控制器的方法,如下所示: 路线:: resource('posts','PostController');

However, it also outlines a way of creating Resource controllers in the routes.php file like so: Route::resource('posts', 'PostController');

PostController.php中,我们定义方法时不使用HTTP动词作为前缀.

And in PostController.php we define methods without prefixing it with the HTTP verb.

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?

此外,我们应该使用Route::controller('posts', 'PostController');还是Route::resource('posts', 'PostController');将路由传递到控制器,还是应该手动定义每条路由,如下所示:

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::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');

推荐答案

以该控制器为例:

<?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');

并执行

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检查控制器并根据其找到的方法自动生成路由.

Laravel inspects the controller and generates routes based on what methods it finds, automatically.

但是,如果您这样做

Route::resource('tests', 'TestController');

您将获得此路线清单:

+--------+--------------------------------------------+------------------------+-----------------------------------+----------------+---------------+
| 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路由列表,您可以删除其中一些路由,但是它不会检查您的控制器来为您的方法构建路由.

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.

您可以决定最适合自己的事物.但是,通常,如果您的控制器是CRUD控制器,那么Route :: resource()是一个好的开始,否则您可以使用Route :: controller()或手动构建路由.

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.

真的没有一个为什么或为什么另一个仅仅是设计和选择的问题.有些人永远不会使用它们.只是Route::resource()遵循了Rails的路由方式: http://guides.rubyonrails.org/routing. html .

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()并不需要创建所有这些方法,但是最终会得到无意义路由的列表,因为Laravel始终默认创建所有这些方法,除非您这样做:

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('photo', 'PhotoController',
                array('only' => array('index', 'show')));

并且您的路线列表将仅显示索引并显示操作.

And your list of routes will show only the index and show actions.

此外,如果您需要一些其他路线,则必须使用Route::resource()手动构建它们,或运用一些魔术手段将它们自动用于所有资源丰富的路线.使用Route::controller(),一切都是自动的,每次添加新方法时,都会为您创建一条新路线.

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.

同样,如果要构建CRUD控制器,请使用Route::resource()开始.否则,请考虑您自己的情况下一个或另一个的好处.

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.

这是一篇很棒的文章,来自Phil Sturgeon(PyroCMS和PHP-FIG),介绍了手动构建所有路线的好处:

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.

这篇关于Laravel 4定义RESTful控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆