3

I have a route like this:

Route::group(['prefix'=>'admin'],function(){
    Route::resource('users','UserController'); // <-- what is the name of this route
});

How can I address the users route by its name e.g route('users')? I tested users and admin.users but it didn't help.

Muhammad
  • 6,725
  • 5
  • 47
  • 54
Mostafa Solati
  • 1,235
  • 2
  • 13
  • 33

4 Answers4

8

When you register a resourceful route to the controller Route::resource(...), this simple declaration creates multiple routes to handle a variety of actions (list of all actions).

So inside admin route group you will have such names for routes:

  • admin.users.index for GET admin/users
  • admin.users.store for POST admin/users
  • admin.users.show for GET admin/users/{id}
  • etc ...

Basically, you can list all route names using php artisan route:list

  • this is a good answer, i wonder why op did not mark this as an answer. – Bagus Tesa Nov 30 '15 at 23:45
  • 1
    I wonder why this doesn't work for me in laravel 5.4 :/ the resource under prefix doesn't get the prefix in the route name – Srneczek Jun 28 '17 at 10:14
  • @Srneczek, In laravel 5.4 you should explicitly name your group (if you want). [This answer](https://stackoverflow.com/a/36838375/2353034) should help you to figure out details. – Dmytro Krytovych Jun 30 '17 at 03:27
  • the op wants to access the route via its name `route('users')`, not by the url. I am having the same problem – Muhammad Jun 03 '19 at 08:33
3

use this

Route::group(['as'=>'admin.'  ,'prefix'=>'admin'],function(){
    Route::resource('users','UserController'); 
});

the name of route will be

admin.users.index
admin.users.create
admin.users.store
admin.users.edit
admin.users.update
admin.users.destroy
admin.users.show
barbsan
  • 3,418
  • 11
  • 21
  • 28
2

It's admin/users.

The url of the controllers in group will be group prefix then controller resource name. prefix/resource

David Barker
  • 14,484
  • 3
  • 48
  • 77
Dilip Kumar
  • 2,504
  • 2
  • 17
  • 33
0

By using php artisan route:list command print all the routs to the terminal and see its names. it will show users.index, users.create, users.update and so on. usage:

route('users.index');
//output: example.com/admin/users
Muhammad
  • 6,725
  • 5
  • 47
  • 54