How to create another update function with PUT and PATCH request in one route?
I am currently developing a project using laravel 5.0. Lets say i have a route like this in my route.php:
Route::resource('user', 'UserController', ['except' => ['index', 'create', 'store', 'destroy']]);
Route::get('user/{user}/posts', '[email protected]');
Route::get('user/{user}/changepassword', '[email protected]');
Route::put('user/{user}/changepassword', '[email protected]');
Route::patch('user/{user}/changepassword', '[email protected]');
Where if i access http:// localhost:8000/user/{username}
it will trigger the show method, and if i access http://localhost:8000/user/{username}/edit
it will trigger the edit method which will give a PUT
& PATCH
request to http:// localhost:8000/user/{user}
. But in this phase, the user can only edit their personal informations, for the password i want to create a new editPassword
which gives also PUT
& PATCH
request. And im not sure if i write the route correctly above.
So, the question is how do i manually write the route in the route.php file according to laravel's convention?
And should i send the PUT
& PATCH
request to http: //localhost:8000/user/{user}
again (which i think will crash with the PUT
& PATCH
request from the edit function) or should i send the PUT
& PATCH
request to http: //localhost:8000/user/{user}/changepassword
?
Thanks in advance. :)
Answer
First you don't need to repeat patch
function. It's enough to use put
. The second thing is, whenever you create any extra urls and user resource, you need to put them before resource route, so your routes file should look like this:
Route::get('user/{user}/posts', '[email protected]');
Route::get('user/{user}/changepassword', '[email protected]');
Route::put('user/{user}/changepassword', '[email protected]');
Route::resource('user', 'UserController', ['except' => ['index', 'create', 'store', 'destroy']]);
So now, when user go to edit page, they will have link to edit password page, when they click on it they will go to GET user/{user}/changepassword
and when they fill form and click update they will go to PUT user/{user}/changepassword'
Related Questions
- → OctoberCMS Rain User plugin not working or redirecting
- → Good solution to work with rest-api like SPA with redux?
- → Transformer usage on laravel/dingo API
- → Ember.js JSON API confusion
- → Express - better pattern for passing data between middleware functions
- → Checking passed object existence in Laravel delayed queue job
- → Restrict .htm Pages and Partials in OctoberCMS with Nginx
- → Nested array validation laravel
- → Uploading a file and sending it to the backend with React and Node
- → Javascript onClick not firing when assigned inside of array.map
- → How to replace the 3rd product with an Image in product list for Perstashop?
- → How should I implement canonical urls in my prestashop theme?
- → Best way to do front-end public and restricted area with October CMS?