Laravel 4-Route :: resource vs Route :: controller使用哪个? [英] Laravel 4 - Route::resource vs Route::controller. Which to use?

查看:74
本文介绍了Laravel 4-Route :: resource vs Route :: controller使用哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解资源控制器可以采用以下方法

I understand that a Resource controller can have the following methods

index
show
create
edit
store
update
destroy

现在,除了资源操作之外,我还需要执行以下操作:

Now suppose I have the following actions which need to be performed in addition to the resource actions:

  • 用户尝试登录.
  • 管理员希望通过电子邮件/名字查找用户
  • 用户requests之以鼻地请求帖子

资源控制器对上述功能有用吗?如果要编程一个API,我显然要索引,显示,编辑,创建,销毁...还要登录,查找,搜索等...

Are resource controllers useless for the above functionality? If programming an API, I obviously want the index, show, edit,create,destroy... but also the login, find, search etc...

是否可以同时路由到两种类型的控制器?例如

Is it possible to route to both types of controller? e.g.

Route::group(['prefix' => 'api'], function() {
    Route::group(['prefix' => 'v1'], function() {
        // Resource Controller
        Route::resource('posts', 'Api\V1\PostsResourceController');

        // Restful Controller
        Route::controller('posts', 'Api\V1\PostsController');
    });
});

还是我应该忘掉资源控制器,而改用静态控制器?

Or should I just forget about the resource controller and use a restful controller instead?

推荐答案

只需使用资源控制器,

Just use a resource controller, add those other methods to that same controller, and add routes to those methods directly:

Route::group(['prefix' => 'api'], function()
{
    Route::group(['prefix' => 'v1', 'namespace' => 'Api\V1'], function()
    {
        // Add as many routes as you need...
        Route::post('login', 'PostsResourceController@login');
        Route::get('find',   'PostsResourceController@find');
        Route::get('search', 'PostsResourceController@search');

        Route::resource('posts', 'PostsResourceController');
    });
});

P.S.我通常回避使用Route::controller(). 它太含糊了.

P.S. I generally shy away from using Route::controller(). It's too ambiguous.

这篇关于Laravel 4-Route :: resource vs Route :: controller使用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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