Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel, Auth::user() in controller

Tags:

php

laravel

Laravel framework- Why aren't I able to use Auth::user(), (to see if user has been logged in) in the controller of the laravel project. Is the Session not connected to the controller?

HomeController.php

 public function isauthorized(){
    if (Auth::user()){
        return View::make('home.basic')
            ->with('basic1', 'Authorized');
    }else{
        return View::make('home.basic')
            ->with('basic1', 'Not Authorized');
    }
}

My login system is working and the session is always set when logged in. When I call the function Auth::user() in the View or in routes.php, it is succesfull. But when I call the function in the controller, it is false. Help please.

like image 200
roy Avatar asked Jul 30 '14 02:07

roy


1 Answers

You need to use the Auth facade in the controller;

add this line just before class definition.

use Auth;
like image 162
gxet4n Avatar answered Nov 07 '22 07:11

gxet4n