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?

Community
  • 1
  • 1
Rafael
  • 557
  • 6
  • 21

1 Answers1

4

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.

jszobody
  • 28,495
  • 6
  • 61
  • 72
  • Too bad there isn't a real way, but I didn't think of partials. Thanks a bunch! – Rafael Aug 15 '16 at 20:43
  • 1
    Well this answers the question but yuk! Either use the `::resource` shortcut or declare all the routes, its a lot easiers to understand whats going on that way – Steve Aug 15 '16 at 21:49
  • 1
    @Steve I agree completely. I've stopped using `resource` myself and specify all my routes explicitly. I like to see exactly what's happening. – jszobody Aug 15 '16 at 22:28
  • I hear you both. I'm on v5.5 and encountering the same issue. will go on the same route: not using `resource` and defining my routes explicitly – suo Oct 09 '17 at 14:51