Reputation: 178
So I decided to change the look of the urls in my application. The current look is something like that: /user/profile
. This is the page the user sees, after he's logged in.
I want to pass the $username
to the url, something like this: /name_of_the_user/profile
This is what I tried so far:
Route:
Route::get('/{user}/profile', [
'uses' =>'UserController@getProfile',
'as' => 'user.profile',
'middleware' => 'auth'
]);
Link:
<a href="{{ route('user.profile',auth()->check() ? auth()->user()->username : 'default') }}">User Profile</a>
This is partially working.
When the user logs in, he gets this error:
UrlGenerationException in UrlGenerationException.php line 17: Missing required parameters for [Route: user.profile] [URI: {user}/profile].
But if I reload the page its ok. The username passed successfully to the url as it supposed to.
How can I fix this error?
I should say that a user goes to /user/signin
to sign in and he is being redirected to /user/profile
Any help is appreciated!
Upvotes: 1
Views: 1294
Reputation: 2069
<a href="{{ route('user.profile', Auth::user()->username) }}">User Profile</a>
If not working :
<a href="{{ route('user.profile', Auth::user()->username ? : 'nousernamefound') }}">User Profile</a>
Upvotes: 0
Reputation: 163978
You need to add some default value for users who are not logged in:
auth()->check() ? auth()->user()->username : 'default'
If you want to redirect not logged in users, use auth
middleware or custom middleware for that.
Upvotes: 1