Laravel 5.3 auth check in constructor returning false
docs
you can't access the session or authenticated user in your
controller's constructor because the middleware has not run yet.As an alternative, you may define a Closure based middleware directly
in your controller's constructor. Before using this feature, make sure
that your application is running Laravel 5.3.4 or above:
class ProjectController extends Controller
{
/**
* All of the current user's projects.
*/
protected $projects;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->projects = Auth::user()->projects;
return $next($request);
});
}
}
Laravel Auth : Returning false in controller
u just check in controller by middleware
not auth
check like that not Auth::check()
try to check by middleware
public function __construct()
{
$this->middleware('auth');
}
becuase middleware is not running before auth so always throw false in constructer but u get true in any controller method .
try to check Auth::check()
in controller method more detail see
Access Auth in parent constructor in Laravel 5.3
This is because of changes in Laravel 5.3. If you need to use Auth in constructor you need to do it like so:
public function __construct()
{
$this->middleware(function ($request, $next) {
if(\Auth::check() ) {
$this->user = \Auth::user();
}
var_dump($this->user);
return $next($request);
});
}
This change was described in upgrade guide to Laravel 5.3 - https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0 look at section Session In The Constructor
Lavarel Auth::check() not woking
Laravel 5.3 auth check in constructor returning false
you can't access the session or authenticated user in your
controller's constructor because the middleware has not run yet.As an alternative, you may define a Closure based middleware directly
in your controller's constructor. Before using this feature, make sure
that your application is running Laravel 5.3.4 or above:
Can't call Auth::user() on controller's constructor
See here:
Session In The Constructor
In previous versions of Laravel, you could access session variables or
the authenticated user in your controller's constructor. This was
never intended to be an explicit feature of the framework. In Laravel
5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.As an alternative, you may define a Closure based middleware directly
in your controller's constructor. Before using this feature, make sure
that your application is running Laravel 5.3.4 or above:<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
class ProjectController extends Controller
{
/**
* All of the current user's projects.
*/
protected $projects;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->projects = Auth::user()->projects;
return $next($request);
});
}
}
Of course, you may also access the request session data or
authenticated user by type-hinting the Illuminate\Http\Request class
on your controller action:/**
* Show all of the projects for the current user.
*
* @param \Illuminate\Http\Request $request
* @return Response
*/
public function index(Request $request)
{
$projects = $request->user()->projects;
$value = $request->session()->get('key');
//
}
laravel - Can't get session in controller constructor
You can't do it by default with Laravel 5.3. But when you edit you Kernel.php
and change protected $middleware = [];
to the following it wil work.
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
Hope this works!
laravel 5.4 : cant access Auth::user() in the __construct method
As the doc says :
In previous versions of Laravel, you could access session variables or
the authenticated user in your controller's constructor. This was
never intended to be an explicit feature of the framework. In Laravel
5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.
So try this :
public function __construct()
{
$this->middleware('auth');
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
Controller return `false` while DashboardController return `true`
As of Laravel 5.3, you can't access the session (including Auth) in the controller constructor. You can, however, define a middleware closure in the constructor that will have access to the session.
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct()
{
$this->middleware(function ($request, $next) {
dd(Auth::check());
});
}
}
Laravel auth gurad check is not working in constructor
Since Laravel 5.3 you are no longer able to access session (and thus Auth
stuff as well) in controller constructors, because session middleware has not run yet.
5.3 changes - scroll to "Session In The Constructor" to see how to get around it.
Related Topics
How to Override Trait Function and Call It from the Overridden Function
How to Use Mamp's Version of PHP Instead of the Default on Osx
Stop Caching for PHP 5.5.3 in Mamp
Best Practices: Working with Long, Multiline Strings in PHP
File_Get_Contents Behind a Proxy
Split String by Delimiter, But Not If It Is Escaped
Back to Previous Page with Header( "Location: " ); in PHP
Send Email with PHPmailer - Embed Image in Body
Laravel Recursive Relationships
You Must Enable the Openssl Extension to Download Files via Https
How to Get Customer Details from an Order in Woocommerce
How Does the "&" Operator Work in a PHP Function
Select Entries Between Dates in Doctrine 2
Laravel Preg_Match(): No Ending Delimiter '/' Found