mardi 2 février 2016

Does a Laravel request need to use the Auth middleware to have access to the Auth facade?

I'm working with my first real Laravel application and I'm having trouble getting the Auth facade to work the way I expect.

In my routes.php file, I am attempting to use Auth::check() to determine if the user hitting the route '/'is currently authenticated (to change which view the user sees), but it is not returning any value.

Route::get('/', function() { 
    if (Auth::check()) { 
        return view('home'); 
    } else { 
        return view('welcome');
})

I suspect this is because the authentication (Auth facade, auth()) is only usable in closures utilizing the auth middleware, but examples I have seen online don't seem to have that problem. I also wonder if it's because the routes.php file doesn't identify any namespace or use imports, but, again, examples I've seen don't use anything either.

Log::debug(Auth::check());    // returns nothing
Log::debug(auth()->user());   // returns nothing

I ended up just modifying the authentication controller to check the path of the incoming request, and if it was '/', then redirect based on the user's authentication status there (because there I could actually access Auth functionality), but I want to know what I'm missing. Can the routes.php file access Auth information without hiding the entire route behind the auth middleware?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire