Search code examples
phplaravellaravel-4

Laravel named route for resource controller


Using Laravel 4.2, is it possible to assign a name to a resource controller route? My route is defined as follows:

Route::resource('faq', 'ProductFaqController');

I tried adding a name option to the route like this:

Route::resource('faq', 'ProductFaqController', array("as"=>"faq"));

However, when I hit the /faq route and place {{ Route::currentRouteName() }} in my view, it yields faq.faq.index instead of just faq.


Solution

  • 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 running php artisan route:list in your terminal/console. You can also see the types of route names generated on the resource controller docs page.

    Here are the ways you can modify the names for resource controller routes:

    Change a single action's name entirely:

    Route::resource('faq', 'ProductFaqController')
        ->name('index', 'list');
    
    // GET faq -> list
    

    Note that the original faq. prefix is NOT included when using name() on a single resource method. You have to replace this yourself.

    Change multiple actions' names entirely:

    You can call name() multiple times fluently:

    Route::resource('faq', 'ProductFaqController')
        ->name('index', 'faq.list')
        ->name('destroy', 'faq.delete');
    
    // GET    faq       -> faq.list
    // DELETE faq/{faq} -> faq.delete
    

    OR, you can call names() and pass an array of methods and their names:

    Route::resource('faq', 'ProductFaqController')
        ->names([
            'index' => 'faq.list',
            'destroy' => 'faq.delete',
        ]);
    
    // GET    faq       -> faq.list
    // DELETE faq/{faq} -> faq.delete
    

    Note that this also replaces the entire name and does not include the original faq. prefix. I included it here as an example of keeping the faq. prefix but changing the method name.

    Change only the resource segment of all method names:

    Call the names() method, passing a string of the desired resource segment instead of an array:

    Route::resource('faq', 'ProductFaqController')
        ->names('productfaq');
    
    // GET  faq -> productfaq.index
    // POST faq -> productfaq.store
    // etc...
    

    The new segment can include periods if it makes sense for your organization:

    Route::resource('faq', 'ProductFaqController')
        ->names('product.faq');
    
    // GET  faq -> product.faq.index
    // POST faq -> product.faq.store
    // etc...
    

    Add the same prefix to every route name:

    See Route Name Prefixes:

    Route::name('prefix')->group(function () {
        Route::resource('faq', 'ProductFaqController');
    });
    
    // GET  faq -> prefix.faq.index
    // POST faq -> prefix.faq.store
    // etc...
    

    Add a URI prefix and route name prefix to the whole resource:

    See Route Group Prefixes, and include an as() call to modify the route names:

    Route::prefix('admin')->as('admin.')->group(function () {
        Route::resource('faq', 'ProductFaqController');
    });
    
    // GET  admin/faq -> admin.faq.list
    // POST admin/faq -> admin.faq.create
    // etc...