Laravel named route for resource controller

When you use a resource controller route, it automatically generates names for each individual route that it creates. Route::resource() is basically a helper method that then generates individual routes for you, rather than you needing to define each route manually.

You can view the route names generated by typing php artisan routes in Laravel 4 or php artisan route:list in Laravel 5 into your terminal/console. You can also see the types of route names generated on the resource controller docs page (Laravel 4.x | Laravel 5.x).

There are two ways you can modify the route names generated by a resource controller:

  1. Supply a names array as part of the third parameter $options array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.

    Route::resource('faq', 'ProductFaqController', [
        'names' => [
            'index' => 'faq',
            'store' => 'faq.new',
            // etc...
        ]
    ]);
    
  2. Specify the as option to define a prefix for every route name.

    Route::resource('faq', 'ProductFaqController', [
        'as' => 'prefix'
    ]);
    

    This will give you routes such as prefix.faq.index, prefix.faq.store, etc.

Leave a Comment