96

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.

12 Answers 12

256

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...
Sign up to request clarification or add additional context in comments.

5 Comments

Hi I like this solution, but I wanna do it with controller route. Is there any way to do something like this: Route::controller('/', 'HomeController', ['names' => ['home.index' => 'getIndex']]);
@AndersonNunesdaSilva The solution is very similar to that, and is described in the documentation.
@Aken Roberts Maybe you can help me. Look at this : stackoverflow.com/questions/49594340/…
I didn't know about as key, thank you. Cleanest solution
The answer No. 2 is the best! I used it just after reading and it helped even in Laravel 8. Thank you!
43

For answer seekers with Laravel 5.5+ finding this page:

Route::namespace('Admin')->prefix('admin')->name('admin.')->group(function () {

    Route::resource('users','UserController');

});

These options will result in the following for the Resource:

  • namespace() sets Controller namespace to \Admin\UserController

  • prefix() sets request URi to /admin/users

  • name() sets route name accessor to route('admin.users.index')

In name() the DOT is intended, it is not a typo.

Please let others know if this works in comments for any versions prior to Laravel 5.5, I will update my answer.

Update:

Taylor accepted my PR to officially document this in 5.5:

https://laravel.com/docs/5.5/routing#route-group-name-prefixes


UPDATE LARAVEL 8

New in Laravel 8, the need to use a namespace in route configs is deprecated, the default namespace wrapper in RouteServiceProvider has been removed from Laravel's standard config. This change decouples Controller namespaces from having to be considered when grouping routes, dropping the namespace requirement when registering routes gives much more freedom when organizing controllers and routes.

With Laravel 8, the original example in the top half of this post would now look as follows using self references to static class name:


use \App\Http\Controllers\Admin\{
    UserController,
    ProductController,
    AnotherController,
}

Route::prefix('admin')->name('admin.')->group(function () {

    Route::resource('users', UserController::class);

    Route::resource('products', ProductController::class);

    Route::resource('another', AnotherController::class);

});

1 Comment

Maybe you can help me. Look at this : stackoverflow.com/questions/49594340/…
32

I don't know if it's available in laravel 4.2 (I tested in 5.7) but you can use names to change the name of all routes generated by resource

Route::resource('faq', 'ProductFaqController', ['names' => 'something']);

and the result will be like this

something.index

and you don't need to specify each route

2 Comments

This is a really underrated answer.
where do you find this answer? is it in the documentation?
9

All Updates Later then Laravel 5.5 Using

Route::resource('faqs', 'FaqController', ['as' => 'faqs']);

if we not use ['as' => 'faqs'] in above code then it will also work same.

[Updated]

Important to keep in mind that this will work for "resource"

For example:

Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);

and result will be like

 POST      | admin/posts/tags                  | posts.tags.store
 GET|HEAD  | admin/posts/tags                  | posts.tags.index
 GET|HEAD  | admin/posts/tags/create           | posts.tags.create
 DELETE    | admin/posts/tags/{tag}            | posts.tags.destroy
 PUT|PATCH | admin/posts/tags/{tag}            | posts.tags.update
 GET|HEAD  | admin/posts/tags/{tag}            | posts.tags.show
 GET|HEAD  | admin/posts/tags/{tag}/edit       | posts.tags.edit

Comments

5

Tested with Laravel 8:

You can define your name for resource route as passing names as optional arguments. For Example:

use App\Http\Controllers\UsersController;

Route::resource('reservations', UsersController::class, ['names' => 'users']);

The above example defines routes such as users.index, users.store etc.

You can also pass the route names as:

Route::resource('reservations', UsersController::class, ['names' => 'admin.users']);

which will define routes with prefix of admin such as admin.users.index, admin.users.store

1 Comment

on my end this does not generate the correct routes. for example the show route is generated as | GET|HEAD | admin/clients/{} --> empty curly brackets brings issues
4

Using Laravel 5.5

Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);

important to keep in mind the "resource"

For example, I send something from my project:

Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);

Comments

4

And In Laravel 8

Route::resource('product', 
    App\Http\Controllers\API\Admin\ProductController::class, [
    'names' => [
        'index' => 'admin.product.index', 
        'store' => 'admin.product.store', 
        'update' => 'admin.product.update', 
        'destroy' => 'admin.product.delete'
    ]
])->except(['edit', 'create']);

Comments

2
Route::resource('nice-books', BookController::class)->names('books');

In this line, ->names('books') serves as a books. name prefix to BookController resource routes.

Example effect: route('books.index') -> GET /nice-books

2 Comments

You should add explanation.
@SuperKai-KazuyaIto added. is everything clear now?
1

Route::resource('articles','ArticleController', ['names' => 'xyz'])

Comments

-1

you dont need to set name in resource in laravel 5.7 that i have test it. it auto generate route name from url.

Comments

-1

Got the same error as you. for me it worked by adding the whole path that is namespace/ControllerName

Route::resource('staffs', 'App\Http\Controllers\StaffController');

Comments

-2

You can rename your resource routes in AppServiceProvider.php like so:

public function boot()
{
    Route::resourceVerbs([
        'create' => 'neu',
        'edit' => 'bearbeiten',
    ]);
}

I believe this feature is designed for localization.

1 Comment

Did you actually read the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.