【问题标题】:Laravel 4 nested resource controllers Route::resource('admin/photo', 'PhotoController'); not workingLaravel 4 嵌套资源控制器 Route::resource('admin/photo', 'PhotoController');不工作
【发布时间】:2013-01-13 04:27:23
【问题描述】:

在 Larvel 4 中,我正在尝试设置嵌套资源控制器。

routes.php

Route::resource('admin/photo', 'Controllers\\Admin\\PhotoController');

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) 和 store (/admin/photo POST ) 操作工作正常...但不是 editshow,我只是得到一个页面未找到 404 状态。

如果我删除管理员根路径,它会起作用。

谁能告诉我如何设置 Route::resource 控制器以使用像 admin/photo 这样的嵌套路径

【问题讨论】:

    标签: laravel laravel-4


    【解决方案1】:

    https://github.com/laravel/framework/issues/170 在那里找到了我的答案(看看 Taylor 写了什么)

    对于那些想查看我现在在 routes.php 中工作的代码的人:

    Route::group(array('prefix' => 'admin'), function() {
    
        // Responds to Request::root() . '/admin/photo'
        Route::resource('photo', 'Controllers\\Admin\\PhotoController');
    });
    

    【讨论】:

      【解决方案2】:

      实际上,你应该将“admin/photo”替换为“admin.photo”,以便 laravel 为 photo 作为 admin 的子资源设置资源。

      检查这个 https://tutsplus.com/lesson/nested-resources/

      【讨论】:

        【解决方案3】:

        你可能需要告诉 Composer 重新加载类,从你的命令行运行:

        composer dump-autoload
        

        应该可以的。

        【讨论】:

          【解决方案4】:

          只需使用组前缀 -> admin。使用嵌套的 admin.photo 会为您创建一个错误的 url,例如 admin/{admin}/photo/{photo},这是您不想要的。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-01
            • 2014-08-02
            • 2014-06-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多