Laravel 4 - Route::resource vs Route::controller. Which to use?(Laravel 4 - Route::resource 与 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:
- 用户尝试登录.
- 管理员希望通过电子邮件/名字查找用户
- 用户请求发帖
资源控制器对上述功能无用吗?如果对 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', 'ApiV1PostsResourceController');
// Restful Controller
Route::controller('posts', 'ApiV1PostsController');
});
});
或者我应该忘记资源控制器并使用宁静的控制器来代替?
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' => 'ApiV1'], 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 与 Route::controller.使用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 4 - Route::resource 与 Route::controller.使用哪个?
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01