0

Problem

Laravel's method Auth::check() is returning false in the __construct method of my controller but its returning true in any other method used (While i'm logged in of course). I'm assuming the Auth facade isn't quite ready for use when the program hits the __construct method? Or something like that.

For reference Request::path() is working correctly in the same place that Auth::check() returns false.

Examples

public function __construct()
{
    dd(Auth::check()); // Returns False
    dd(Request::path()); // Returns full path as expected
}

public function dashboard()
{
    dd(Auth::check()); // Returns True
}

Question

How do I make Laravels Auth -> 'Illuminate\Support\Facades\Auth' available for use in the __construct method of my controller.

Kenziiee Flavius
  • 1,918
  • 4
  • 25
  • 57
  • This might be the answer to your problem: [Can't call Auth::user() on controller's constructor](https://stackoverflow.com/questions/39175252/cant-call-authuser-on-controllers-constructor) – vega the antler Feb 20 '19 at 08:29
  • 1
    @vivek_23 thank you for the flow of possible duplicates its really helping – Kenziiee Flavius Feb 20 '19 at 08:30
  • _As an alternative, you may define a Closure based middleware directly in your controller's constructor_ quote from Laravel doc [upgrade to 5.3](https://laravel.com/docs/5.3/upgrade) – HamzStramGram Feb 20 '19 at 08:38
  • @HamzStramGram i tried this and it didn't seem to work for me, i solved this however by creating a middleware for the action that i wanted. – Kenziiee Flavius Feb 20 '19 at 10:53

1 Answers1

0

I've decided to move this to middleware since it seems since Laravel 5.3 you can no longer access Auth in a controllers construct method.

Kenziiee Flavius
  • 1,918
  • 4
  • 25
  • 57