Muhmmad Aziz
Muhmmad Aziz

Reputation: 391

Laravel 5.3 resource: change action

I'm new to laravel, now using v5.3. Is it possible to change the called methods when using route::resource

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

+--------+-----------+---------------------------------+--------------+-----------------------------------------------------------------+--------------+
| Domain | Method    | URI                             | Name         | Action                                                          | Middleware   |
+--------+-----------+---------------------------------+--------------+-----------------------------------------------------------------+--------------+
|        | GET|HEAD  | GameServiceAPI/user             | user.index   | App\Http\Controllers\Interfaces\UserController@index   | web          |
|        | POST      | GameServiceAPI/user             | user.store   | App\Http\Controllers\Interfaces\UserController@store   | web          |
|        | GET|HEAD  | GameServiceAPI/user/create      | user.test    | App\Http\Controllers\Interfaces\UserController@create  | web          |
|        | DELETE    | GameServiceAPI/user/{user}      | user.destroy | App\Http\Controllers\Interfaces\UserController@destroy | web          |
|        | PUT|PATCH | GameServiceAPI/user/{user}      | user.update  | App\Http\Controllers\Interfaces\UserController@update  | web          |
|        | GET|HEAD  | GameServiceAPI/user/{user}      | user.show    | App\Http\Controllers\Interfaces\UserController@show    | web          |
|        | GET|HEAD  | GameServiceAPI/user/{user}/edit | user.edit    | App\Http\Controllers\Interfaces\UserController@edit    | web          |
|        | GET|HEAD  | api/user                        |              | Closure                                                         | api,auth:api |
+--------+-----------+---------------------------------+--------------+-----------------------------------------------------------------+--------------+

I want to call my own methods without using the default (index,show,create,...)

Also what the 'Name' column refer to, I found that I can change it using some options that can be added to resource but I don't know its perpose.

Upvotes: 2

Views: 1791

Answers (4)

S Chisefu
S Chisefu

Reputation: 36

You can't change these methods, but you could disable the ones you want to change and add your own instead:

Route::resource('user', 'UserController', ['except' => ['edit']]);

Route::get('user/{id}/customEdit', 'UserController@customEdit');

Upvotes: 1

You just create your own routes like below:

Available Router Methods

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

E.g:

Route::get('uri-path', [
    'as' => 'route-name', 
    'uses' => 'YourController@index'
]);

Docs

Upvotes: 0

TheAlexLichter
TheAlexLichter

Reputation: 7289

The point of using Resource Controllers is that it provides the basic CRUD methods.

If you want to define your own routes, you need to use Route::get(), Route::post(), Route::any() and so on. It is not possible to change the linked methods of a resource controller.

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

You can't change these methods, you should create your own get, post, put etc routes instead of using resource route.

The name column is a route name. You can set it for any route with as:

Route::get('some-path', ['as' => 'route-name', 'uses' => 'SomeController@comeAction'])

Or with ->name():

Route::get('some-path', `SomeController@comeAction`)->name('route-name');

Upvotes: 0

Related Questions