AL-zami
AL-zami

Reputation: 9066

pass auth::user as data variable with route

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

Upvotes: 0

Views: 541

Answers (4)

Hrach
Hrach

Reputation: 347

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'));
});

Upvotes: 2

Lucas Silva
Lucas Silva

Reputation: 1411

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'));
}

Upvotes: 1

AL-zami
AL-zami

Reputation: 9066

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]);

    });

Upvotes: 0

Devon Bessemer
Devon Bessemer

Reputation: 35337

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.

Upvotes: 0

Related Questions