Support the ongoing development of Laravel.io →
posted 10 years ago
Session

Hi guys, I put the code below in my controller(I'm using Laravel 5):

if(Session::has('user')){ return view('home.index'); }else{ App::abort(404); }

and I got the following error:

Whoops, looks like something went wrong.

1/1 FatalErrorException in HomeController.php line 20: Class 'App\Http\Controllers\Session' not found

  1. in HomeController.php line 20
  2. at HandleExceptions->handleShutdown()

When I use Session::has('user') in the view tho everything looks fine.

Can anyone show me the right way of using Session object in controller?

Many thanks

Last updated 3 years ago.
0

Ok, I got it fixed. I forgot the namespace...

Last updated 3 years ago.
0

Hey nicholascc, what do you mean you forgot the namespace? I'm getting the same error when trying to use Session::flash from a controller (also L5). Any help?

0

thetimmyc said:

Hey nicholascc, what do you mean you forgot the namespace? I'm getting the same error when trying to use Session::flash from a controller (also L5). Any help?

add use Session; line below the opening php tag in your controller to use Session class in laravel 5.

Last updated 10 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

nicholascc nicholascc Joined 14 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.