laravel - Can't get session in controller constructor

后端 未结 8 1737
感动是毒
感动是毒 2020-12-05 19:02

In new laravel I can\'t get session in constructor. Why?

public function __construct()
{
    dd(Session::all()); //this is empty array
}

a

相关标签:
8条回答
  • 2020-12-05 19:16

    In Laravel 5.3 sessions related functionality will not work in the a controller constructor, so you should move all sessions related logic to methods.

    0 讨论(0)
  • 2020-12-05 19:18

    Laravel 5.7 solution

    public function __construct()
    {
    
    $this->middleware(function ($request, $next) {
    // fetch session and use it in entire class with constructor
    $this->cart_info = session()->get('custom_cart_information');
    
    return $next($request);
    });
    
    }
    

    If you want to use constructor for any other functionality or query or data then do all the work in $this->middleware function, NOT outside of this. If you do so it will not work in all the functions of entire class.

    0 讨论(0)
  • 2020-12-05 19:21
     *This one solved mine problem to use session in constructor* 
       $this->middleware(function ($request, $next) {
            if (!session('records_per_page')) {
                session(['records_per_page' => 20]);
            }
    
            // update rows per page
            if (!empty(\Request::get('records_per_page')) && in_array(\Request::get('records_per_page'), [20, 50, 80, 100])) {
                session(['records_per_page' => \Request::get('records_per_page')]);
            }
            return $next($request);
        });
    
    0 讨论(0)
  • 2020-12-05 19:22

    I used Session in the construct using middleware, You can try, It would be helpful

    public function __construct()
    {
        $this->middleware('PM');
    
        $this->middleware(function ($request, $next){
            $school_id = Session::get('school_id');
    
    
            return $next($request);
        });
    }
    
    0 讨论(0)
  • 2020-12-05 19:24

    As of Laravel 6.18.1, add these to $middleware array in the kernel.php

    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    

    And it should work.

    0 讨论(0)
  • 2020-12-05 19:29

    If anyone is coming across this question in now, instead of accessing session values in controller constructors, I will suggest creating a middleware and placing it in the Web middleware section in the App\Http\Kernel.

    This is because sessions are not part of the requests that are passed to Controller constructors. To then do any session value checks or manipulation in every controller, it is safer to create a middleware.

    This is where session values can be accessed as part of the HTTP request going into the route.

    `/**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            ...,
            ...,
            ...,
            ...,
            ...,
            \Illuminate\Session\Middleware\StartSession::class,
            \App\Http\Middleware\MySpecialMiddleWareToAccessSessionVariables::class
        ],
    
        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];`
    

    This is because \Illuminate\Session\Middleware\StartSession has started the session before it hits your routes

    0 讨论(0)
提交回复
热议问题