Laravel 4 嵌套资源控制器 Route::resource('admin/photo', 'PhotoController'); 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14585654/
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 nested resource controllers Route::resource('admin/photo', 'PhotoController'); not working
提问by Arni Gudjonsson
In Larvel 4 I'm trying to setup nested resource controllers.
在 Larvel 4 中,我试图设置嵌套的资源控制器。
in routes.php:
在routes.php 中:
Route::resource('admin/photo', 'Controllers\Admin\PhotoController');
in app\controllers\Admin\PhotoController.php:
在app\controllers\Admin\PhotoController.php 中:
<?php namespace Controllers\Admin;
use Illuminate\Routing\Controllers\Controller;
class PhotoController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return 'index';
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* @return Response
*/
public function show($id)
{
return $id;
}
/**
* Show the form for editing the specified resource.
*
* @return Response
*/
public function edit($id)
{
return "edit $id";
}
/**
* Update the specified resource in storage.
*
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @return Response
*/
public function destroy($id)
{
//
}
}
index(/admin/photo GET), create(/admin/photo/create) and store(/admin/photo POST) actions work fine... but not editand show, I just get a page not found 404 status.
索引(/admin/photo GET)、创建(/admin/photo/create) 和存储(/admin/photo POST) 操作工作正常...但不能编辑和显示,我只是得到一个页面未找到 404 状态。
it will work though if I drop the admin root path.
如果我删除管理员根路径,它将起作用。
Can anyone tell me how I setup the Route::resource controller to work with a nested path like admin/photo
谁能告诉我如何设置 Route::resource 控制器以使用 admin/photo 等嵌套路径
回答by Arni Gudjonsson
See https://github.com/laravel/framework/issues/170Found my answer there (see what Taylor wrote)
请参阅https://github.com/laravel/framework/issues/170在那里找到我的答案(参见 Taylor 写的内容)
For those who want to see my code that works now in routes.php:
对于那些想在 routes.php 中查看我的代码的人:
Route::group(array('prefix' => 'admin'), function() {
// Responds to Request::root() . '/admin/photo'
Route::resource('photo', 'Controllers\Admin\PhotoController');
});
回答by Bradley Weston
In actual fact you should replace the "admin/photo" with "admin.photo" for laravel to set-up a resource for photo being a sub of admin.
实际上,您应该将 laravel 的“admin/photo”替换为“admin.photo”,以便为照片设置资源,使其成为 admin 的子级。
回答by sunzu
Probably you will need to tell Composer to reload classes again, run from your command line:
可能你需要告诉 Composer 重新加载类,从你的命令行运行:
composer dump-autoload
That should work.
那应该有效。
回答by Kun Andrei
Just simply use a group prefix -> admin. Using nested admin.photo will create you an wrong url like admin/{admin}/photo/{photo}, which you don't want.
只需简单地使用组前缀 -> admin。使用嵌套的 admin.photo 会为您创建一个错误的 url,例如 admin/{admin}/photo/{photo},这是您不想要的。