0

So I've read through all the Laravel sessions information and all the stackoverflow questions about Laravel sessions but it's just no help.

Im trying to store the username when I'm logging in, and I want it to show the whole time I'm logged in on a page. Now when I login I get to the main page and the username shows, and I go back to login, which redirects me to the main page and the username is not there anymore. The code I'm using:

Session::put('username', Input::get('username'));
$value = Session::get('username');
return View::make('main')->with('username',$value);

and then in my main.blade.php:

@if(!empty($username))
     {{$username}}
@endif

I have no idea what I'm doing wrong. I'm doing it exactly like all the examples. What am I doing wrong here?

13
  • What do you have in your app/config/session.php? Commented Feb 3, 2015 at 7:59
  • @Dencker There's alot. What do you need to know? Commented Feb 3, 2015 at 8:00
  • Oh, and by the way, the @if... can be shortened to {{ $username or '' }} Commented Feb 3, 2015 at 8:01
  • @Dencker 'driver' => 'file' Commented Feb 3, 2015 at 8:02
  • Okay, now - the "files"-key says storage_path().'/sessions', right? If so, fire up a terminal, go to your project root and type chmod -R a+rwx app/storage. Maybe it's necessary to put sudo in front. Commented Feb 3, 2015 at 8:03

1 Answer 1

2

If you're not doing it already, you should make use of Laravels Auth. Then you don't have to handle the stuff yourself and can just do this to retrieve the username:

Auth::user()->username;

Edit

The actual problem here weren't your sessions but the fact that you only passed the $username to the view in your one controller and not all the time when your rendered it.

If you want some property of the current user, definitely use Auth::user()->username. If you have other session values you can access them directly in the view using:

{{ Session::get('foo'); }}

Another approach are view composers. They let you pass something to the view every time it gets rendered:

View::composer('main', function($view){
    $view->with('foo', Session::get('foo'));
});

You can put this code in app/filters.php or create a new app/composers.php and include it at the end of app/start/global.php with:

require app_path().'/composers.php';

Now the variable always available in main.blade.php by just:

{{ $username }}
Sign up to request clarification or add additional context in comments.

14 Comments

So after the: if(Auth::attempt) I can use: return View::make('main')->with('username',Auth::user()->username); ?
@Dencker It's still only showing the username when I logged in and when i go to login and I get redirected back, it forgets the username again.
You don't need that. You can access Auth::user()->username in your views directly so there's no need to assign it separately.
@MaGnetas But how about data that I don't use in auth. I can just use it like I use in my question?
Well you don't need to work with your sessions at all. Auth will take care of it. You can always check if the user is logged in (Auth::check()) or get the username (Auth::user()->username). The problem in your code is that you set a session value and then get it immediately and reuse it. So basically if your input doesn't have the username value you will not be outputting it :) I recommend skipping the session part and working with Auth directly.
|

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.