Framework: [5.4] Named resource route parameters

Created on 5 Jun 2017  路  9Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.24
  • PHP Version: 7.1.5
  • Database Driver & Version: MySQL 14.14

Description:

The route parameter name is singular when defining resources. However you have to use the plural name when renaming.

https://laravel.com/docs/5.4/controllers#resource-controllers

Steps To Reproduce:

Route::resource('users', 'UserController', ['parameters' => [
    'user' => 'admin_user',
]]);
$ php artisan route:list
| Domain | Method    | URI               | Name          | Action                                      | Middleware |
+--------+-----------+-------------------+---------------+---------------------------------------------+------------+
|        | GET|HEAD  | users             | users.index   | App\Http\Controllers\UserController@index   | web        |
|        | POST      | users             | users.store   | App\Http\Controllers\UserController@store   | web        |
|        | GET|HEAD  | users/create      | users.create  | App\Http\Controllers\UserController@create  | web        |
|        | GET|HEAD  | users/{user}      | users.show    | App\Http\Controllers\UserController@show    | web        |
|        | PUT|PATCH | users/{user}      | users.update  | App\Http\Controllers\UserController@update  | web        |
|        | DELETE    | users/{user}      | users.destroy | App\Http\Controllers\UserController@destroy | web        |
|        | GET|HEAD  | users/{user}/edit | users.edit    | App\Http\Controllers\UserController@edit    | web        |
+--------+-----------+-------------------+---------------+---------------------------------------------+------------+

Most helpful comment

I've always thought of it as referring to the resource that the parameter represents. Here, you're working with 'users', so instead of renaming the 'user' parameter to 'users', you're specifying that the parameter for 'users' should be 'admin_user'. So this makes sense:

Route::resource('users', 'UserController', ['parameters' => [
    'users' => 'admin_user',
]]);

And works as you would expect.

All 9 comments

whats the issue?

It should generate resource routes like this

| Domain | Method    | URI                     | Name          | Action                                      | Middleware |
+--------+-----------+-------------------------+---------------+---------------------------------------------+------------+
|        | GET|HEAD  | users                   | users.index   | App\Http\Controllers\UserController@index   | web        |
|        | POST      | users                   | users.store   | App\Http\Controllers\UserController@store   | web        |
|        | GET|HEAD  | users/create            | users.create  | App\Http\Controllers\UserController@create  | web        |
|        | GET|HEAD  | users/{admin_user}      | users.show    | App\Http\Controllers\UserController@show    | web        |
|        | PUT|PATCH | users/{admin_user}      | users.update  | App\Http\Controllers\UserController@update  | web        |
|        | DELETE    | users/{admin_user}      | users.destroy | App\Http\Controllers\UserController@destroy | web        |
|        | GET|HEAD  | users/{admin_user}/edit | users.edit    | App\Http\Controllers\UserController@edit    | web        |
+--------+-----------+-------------------------+---------------+---------------------------------------------+------------+

I've always thought of it as referring to the resource that the parameter represents. Here, you're working with 'users', so instead of renaming the 'user' parameter to 'users', you're specifying that the parameter for 'users' should be 'admin_user'. So this makes sense:

Route::resource('users', 'UserController', ['parameters' => [
    'users' => 'admin_user',
]]);

And works as you would expect.

According to the docs,

You can easily override this on a per resource basis by passing parameters in the options array. The parameters array should be an associative array of resource names and parameter names.

so, as I assumed, you pass the resource name, in your case 'users' and then the parameter name 'admin_user'.

The example in the docs shows 'user' => 'admin_user', because it is using 'user' as the resource name instead of 'users'.

I've always thought of it as referring to the resource that the parameter represents.

Makes sense when you put it like that.

The issue here was that I spent a good 30 minutes figuring this out. Even this issue was originally about the parameter naming not working at all.

The naming convention here or the documentation is too misleading - just my opinion.

Ran into this, too. I'm chiming in here because 1) it's strange that the {id} parameter naming convention was ignored and 2) the docs compound this by referring to an also unconventionally singular route name ('/user'). If you're an experienced developer and found yourself at this issue after scratching your head for 30 minutes, don't feel bad.

I ended up here because I ran into the same problem.
In my opinion, the naming convention for the example route in the docs is indeed misleading.

Please consider making the functionality a little clearer with an updated example like:

Route::resource('users', 'AdminUserController', ['parameters' => [
    'users' => 'admin_user'
]]);

@martbock omg agreed...just spent like 30mins not seeing that -_-..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

iivanov2 picture iivanov2  路  3Comments