0

I want that when the user click the profile page i want to pass Auth::user()->username as argument to my userController's show method.I have the profile link as following:

<li><a href="{{URL::to('/profile')}}">Profile</a></li>

And in my route i have the following route

Route::get('/profile/{username}',function(){
     return View::make('user.show')->with($username);
});

my question is how i can set username in my '/profile/{username}' as Auth::user()->username when i click the profile link?currently the profile link does not attach any parameter with it

4 Answers 4

2

First of all {{URL::to('/profile')}} is not pointing to Route::get('/profile/{username}) url,there are two different routes

So what you need to do is either change the link , i.e.

{{URL::to('/profile/' . \Auth::user()->username)}}

and then in your route file

Route::get('/profile/{username}',function($username){
    return View::make('user.show')->with(['username' => $username]);
});

//note that you need to pass the array in with() method or you can do this

Route::get('/profile/{username}',function($username){
    return View::make('user.show',compact('username'));
});
Sign up to request clarification or add additional context in comments.

Comments

1

When the user clicks on profile link:

<li>
  <a href="{!! route('user.show', Auth::user()->username) !!}">My Profile</a>
</li>

The UserController@show method is called.

<?php

// routes.php

Route::get('profile/{username}', 'UserController@show')->name('user.show');

// UserController.php

public function show($username)
{
    $user = User::whereUsername($username)->first();

    return view('user.show', compact('user'));
}

and a View response is returned to the user.

@update

If you need is just redirect the control to the UserController@show method, you can do this:

<li>
  <a href="{!! route('user.profile', Auth::user()->username) !!}">My Profile</a>
</li>

<?php

// routes.php

Route::get('profile/{username}', function ($username) {
    return redirect()->route('user.show', Auth::id());
})->name('user.profile');

Now if you want customize the UserController@show action:

<li>
  <a href="{!! route('user.profile', Auth::user()->username) !!}">My Profile</a>
</li>

The UserController@show method is called.

<?php

// routes.php

Route::resource('user', 'UserController', ['except' => ['show']);
Route::get('profile/{username}', 'UserController@profile')->name('user.profile');

Now you can delete the UserController@show method if you want or change the profile method name to show.

// UserController.php

public function profile($username)
{
    $user = User::whereUsername($username)->first();

    return view('user.show', compact('user'));
}

Comments

0

A quick way is to setup a redirect from /profile and it won't break the functionality if they want to view someone else's profile.

Route::get('/profile',function(){
    return Redirect::to('/profile/'.Auth::user()->username);
}

However, I'd recommend doing an Auth::check() before the redirect.

2 Comments

i don't have a page called profile .i have to send it to user.show page
I'm not sure what you mean. My code will just redirect the user and end up using the profile/username route you have in your question.
0

i did something like the following

<li><a href="{{URL::to('/profile')}}">Profile</a></li>

and in route.php:

Route::get('/profile',function(){
        return redirect()->route('user.show',[Auth::user()->username]);

    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.