4
votes

In Larvel 4 I'm trying to setup nested resource controllers.

in routes.php:

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

in 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 edit and show, I just get a page not found 404 status.

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

4

4 Answers

15
votes

See https://github.com/laravel/framework/issues/170 Found my answer there (see what Taylor wrote)

For those who want to see my code that works now in routes.php:

Route::group(array('prefix' => 'admin'), function() {

    // Responds to Request::root() . '/admin/photo'
    Route::resource('photo', 'Controllers\\Admin\\PhotoController');
});
1
votes

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.

Check this https://tutsplus.com/lesson/nested-resources/

0
votes

Probably you will need to tell Composer to reload classes again, run from your command line:

composer dump-autoload

That should work.

0
votes

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.