Rafael Rafael - 3 months ago 28
PHP Question

Route::resource change path names in Laravel 5.2?

When using RESTful resource controllers, we can use route::resource() to work with, but the URIs seem to be default.

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


(ignore the first and the last two, we're looking at the 'items.something' routes)

I want to, for example, change "items/create" to "items/new".
On a previous question, the answer was "No", but since the question is over one year old and Laravel developments seems to be pretty fast, is there already a solution?

Answer

The answer hasn't really changed. You can't customize a resource controller action directly. There is a workaround however: you can exclude it and add it yourself.

First make your resource route partial and exclude the action(s) you don't want:

https://laravel.com/docs/5.2/controllers#restful-partial-resource-routes

Route::resource('items', 'ItemController', ['except' => [
    'create'
]]);

Then you can add your own routes in addition:

https://laravel.com/docs/5.2/controllers#restful-supplementing-resource-controllers

Route::get('items/new', 'ItemController@new');

Make sure to stick that before the resource route, as mentioned in the docs.

Note you can customize the route name, as mentioned in that previous thread you linked to.