I'm building an API for user and admin.
Got stuck at edit user profile routing.
on admin route i use Route::resource('user', 'UserController')
on user route i use Route::get('profile', 'UserController@show')
At the show method Laravel default has
public function show($id)
{
}
the different between them is on admin I can use /id
but on user i check their token from middleware and merge the request to get their user_id
so there is no need for the API to use profile/{id}
.
The question is how can I use the same method but there is an argument to fill and the route still /profile
?
One of my solution is :
public function show($id){
if ($request->has('user_id')):
$id = $request->query('user_id');
endif;
}
It working but when i read the code, it's really redundant always checking it and replace the id.
Just place the request object as a parameter in your controller and get the input from the request object when you use your user route.
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.