Arni Gudjonsson
Arni Gudjonsson

Reputation: 544

Laravel 4 nested resource controllers Route::resource('admin/photo', 'PhotoController'); not working

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

Upvotes: 4

Views: 8978

Answers (4)

Kun Andrei
Kun Andrei

Reputation: 266

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.

Upvotes: 0

sunzu
sunzu

Reputation: 106

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

composer dump-autoload

That should work.

Upvotes: 0

Bradley Weston
Bradley Weston

Reputation: 425

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/

Upvotes: 1

Arni Gudjonsson
Arni Gudjonsson

Reputation: 544

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');
});

Upvotes: 15

Related Questions