Laravel Session Not Working In Constructor

In this tutorial you will learn about the Laravel Session Not Working In Constructor and its application with practical example.

Laravel Session Not Working In Constructor

Back in laravel 5.2, we were able to interact with the session directly in a controller constructor. However, this has changed in laravel 5.3. Now 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. Using an inline middleware, we can directly hook into the request’s routing, so that we have full access to the session:

Laravel Inline middleware

Using an inline middleware, we can directly hook into the request’s routing, so that we have full access to the session:

Example:-

Recommended:-  Laravel 5 Class 'form' not found
In this tutorial we have learn about the Laravel Session Not Working In Constructor and its application with practical example. I hope you will like this tutorial.