Asked  7 Months ago    Answers:  5   Viewed   49 times

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

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

and then below

public function index()
{
    dd(Session::all()); //here works
}

In old laravel i remember there was not this problem. something changed?

 Answers

11

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 = [
    IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,
    IlluminateSessionMiddlewareStartSession::class,
    IlluminateViewMiddlewareShareErrorsFromSession::class,
];

protected $middlewareGroups = [
    'web' => [
        AppHttpMiddlewareEncryptCookies::class,
        IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,

        AppHttpMiddlewareVerifyCsrfToken::class,
        IlluminateRoutingMiddlewareSubstituteBindings::class,
    ],
    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

Hope this works!

Wednesday, March 31, 2021
 
Troncoso
answered 7 Months ago
84

It's worth noting that Laravel actually only stores active sessions in the session folder; those that are 'remembered' but haven't been actively accessing your application do not count among these sessions. As such, just deleting the files won't finish the job.

You must also clear all Remember Tokens from the users table, as this token is used in conjunction with the local token stored in the cookie to re-create the session when a user reconnects.

Wednesday, March 31, 2021
 
muncherelli
answered 7 Months ago
91

In order to get remember me functionality you shouldn't work with session configuration. Instead, you should use the functionality already built into Laravel.

When you authenticate user with Auth::attempt(), you can pass a boolean value as the second argument. Set it to true, if user checked remember me checkbox. Set it to false otherwise.

This way, when checkbox is checked, user will be remembered till they manually log out of application. Otherwise, they'll be remembered till they close the browser or session expires.

You can find some more informaton here: https://laravel.com/docs/5.3/authentication#remembering-users

Wednesday, March 31, 2021
 
mgraph
answered 7 Months ago
42

You need to use the Auth facade in the controller;

add this line just before class definition.

use Auth;
Saturday, May 29, 2021
 
shin
answered 5 Months ago
88

If you have the subdomains hosted on different physical machines, setting the domain in app/config/session.php to this:

'domain' => '.domain.com'

will work as long as the two apps will be able to access a shared session data storage (for example using the database session driver and having a common database that stores the sessions).

Tuesday, August 3, 2021
 
msg
answered 3 Months ago
msg
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :