Laravel - Can't Get Session in Controller Constructor

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!

How to access session in __construct?

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.

In your controller file

public function __construct()
{
$this->middleware(function ($request, $next) {
$t = Translation::where('language_id',Session::get('language_id')->get();
View::share('t', $t);Session::get('language_id');

return $next($request);
});
}

Cant access session in constructor Laravel 5.6

Since Laravel 5.3 you are no longer able to access session in controller constructors, because middleware has not run yet.

You can define a closure (scroll to "Session In The Constructor") that happens after the session middleware has run.

laravel 5.4 controller constructor could not get session variable value even it exist in another method from same controller

It's a pretty common question. This is because the session middleware has not run yet which returns blank.

See here or here.

Explain how to use Session In The Constructor Laravel 5.3

The Laravel docs state that you can't access middleware anymore in the constructor, because it hasn't been loaded yet.

By using that specific Closure, you're actually forcing php (and Laravel) to load whatever logic you have in the Closure as middleware. Take a look at the basic controller class provided by Laravel and see if you can connect the dots.

Essentially, you're hacking the framework.

That being said, it's really bad practice and you shouldn't temper with your session in controller's constructors.

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');

//
}

How to get current user id in constructor in laravel?

You can only access the session in the closure. Just refactor your code to this:

public function __construct()
{
$this->middleware('auth');
$this->middleware(function ($request, $next) {
$this->id = Auth::user()->id;
dd($this->id);

return $next($request);
});
}

You can now use the value $this->id in your controller methods.

In the example in your question, after you've set the value $this->id, you continue with the request. Since you try to access $this->id outside of the scope of the closure, it still is null in the datadump.

Access session data from parent controller w/o passing it in

The reason it won't be working in your __construct() method is because the StartSession middleware won't have been run yet.

To get around this you can simply use the middleware() method on the controller:

public function __construct()
{
$this->middleware(function ($request, $next) {

$userdata = $request->session()->get('userdata');

view()->share(compact('userdata'));

return $next($request);
});
}


Laravel 5.3 Upgrade guide (Scroll down the Controllers section)

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.



Related Topics



Leave a reply



Submit