rebellion
rebellion

Reputation: 6740

Custom route for edit method without ID in URL

I want to make a settings screen for my users, and I want the URL to be easy an memorable: domain.com/user/settings

I want this route to point use the UserController@edit method, however, the edit() method requires ID parameter.

Is there someway I can use user/settings URL without specifying the ID in the URL, but still use the same edit() method in the UserController?

Upvotes: 0

Views: 773

Answers (2)

smartrahat
smartrahat

Reputation: 5609

The edit URL could be use without any parameter. In that case you can mention the user in controller.

Route

Route::get('user/settings');

Controller

public function edit()
{
    $user = Auth::user();
}

Upvotes: 1

rebellion
rebellion

Reputation: 6740

Seems like I managed to solve it?

web.php:

Route::get('user/settings', 'UserController@edit')->name('user.settings');
Route::resource('user', 'UserController');

UserController.php:

public function edit(User $user = null)
{
    $user = Auth::user();
}

Upvotes: 0

Related Questions