且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 4定义RESTful控制器

更新时间:2023-02-12 15:41:02

以该控制器为例:

<?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.