Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mintis's avatar

Laravel 5.4 - auth groups session and old() are empty

Login itself works fine, but after post method I don't see any errors in a blade file. {{ print_r(Session::all()) }} gives empty arrays too.

My Kernel file:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \Corp\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \Illuminate\Session\Middleware\StartSession::class,
];

protected $middlewareGroups = [
    'web' => [
        \Corp\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
     \Illuminate\Session\Middleware\AuthenticateSession::class,  is commented!!!
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Corp\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

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

My route file:

Route::get('login', ['uses'=>'Auth\LoginController@showLoginForm', 'as'=>'login']); Route::post('login', 'Auth\LoginController@login'); Route::get('logout', 'Auth\LoginController@logout');

Route::group(['prefix'=>'admin', 'midlleware'=>['auth']], function (){ Route::get('/', ['uses'=>'Admin\IndexController@index', 'as'=>'adminIndex']); Route::resource('/articles', 'Admin\ArticleController', [ 'names' => [ 'index' => 'admin.articles', 'create' => 'admin.articles.create', 'store' => 'admin.articles.store', ] ]); });

0 likes
15 replies
Paschal's avatar

Is this route file web.php and you are sure your Laravel version is 5.4?

I recently had this issue when building a custom package and it turns out it was a problem with middlewares. My routes were not in the web middleware.

This is what you'll do, run this artisan command:

php artisan route:list

POst the output here let me see

Paschal's avatar

Your route seems to have the web middleware. Dump this variable in your login view and tell me what you get. Make sure you input the wrong details in the login form so it can generate errors.

print_r( $errors )
Mintis's avatar

I see this: Illuminate\Support\ViewErrorBag Object ( [bags:protected] => Array ( ) ) 1

Paschal's avatar

@Mintis it's empty. Do you use a validator? If you do, after providing wrong details, immediately after the validation login, dump the validation and see if it passed.

Mintis's avatar

I think the problem is there:

protected $middleware = [ \Illuminate\Session\Middleware\StartSession::class, ];

When this line is commented, everything works, I have errors messages but it gives me an another problem.

It is a father class:

class AdminController extends Controller { protected $user;

public function __construct()
{

        if (!Auth::check()) {
            return redirect()->route('login')->send();
        }


        $this->user = Auth::user();

}

It is a child class:

class IndexController extends AdminController { public function __construct() { parent::__construct();

//dd($this->user);  // it always gives null although I am login in 
dd('test');

}

If I am not login in, the redirect works and then I log in. I go to admin panel and I am always redirected to '/' (home) page. If I comment this if in father's construction I see 'test' from child class but I lost the log in form, I go direct to admin page.

Mintis's avatar

I found a solution. I created a public function that does this:

if (!Auth::check()) { return redirect()->route('login')->send(); } $this->user = Auth::user();

But it isn't so convenient because it would be better the contructor would do that because now I have to call this method very often.

It there any better solution? Why can't the contructor do this job?

Snapey's avatar

have you fixed this typo?

Route::group(['prefix'=>'admin', 'midlleware'=>['auth']],

Mintis's avatar

Yes, I have fixed it now but the problem is still there with constructor.

I created a Closure in it, but $this->user is always null.

    $this->middleware(function (Request $request, $next) {
        if (!Auth::check()) {
            return redirect()->route('login')->send();
        }
        $this->user = Auth::user();
        return $next($request);
    });
Snapey's avatar

You mean Auth::check() returns true but Auth::user() is null?

Mintis's avatar

class AdminController extends Controller {

protected $user;


public function __construct()
{

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

        if (!Auth::check()) {
            return redirect()->route('login')->send();
        }

        $this->user = Auth::user();


        return $next($request);
    });

}

child class

class IndexController extends AdminController { public function __construct() {

    parent::__construct();

   dd($this->user);                    !!! it gives null

}




public function index(){

!!! it is not null if I comment actions with $this->user in the constructor

dd($this->user); !! it gives obj } }

Mintis's avatar

It finally seems I found a solution.

In the child constructor:

    $this->middleware(function ($request, $next) {
        $this->user = \Auth::user();

    // other actions with user here

        return $next($request);
    });

Please or to participate in this conversation.