How can get the session value on the controller constructor in the laravel – onlinecode

How can get the session value on the controller constructor in the laravel – onlinecode

In this post we will give you information about How can get the session value on the controller constructor in the laravel – onlinecode. Hear we will give you detail about How can get the session value on the controller constructor in the laravel – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Today, I am going to share with you how can get the session value in the controller constructor in laravel. something many people says sometimes we cannot be getting the value of the session in laravel controller constructor but we can get and use session value easily value in another method. so why are we cannot get data directly in the controller constructor because the session middleware has not run yet which returns a blank?

we can not get the session value in below Following the Controller constructor.

PHP
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class ProductController extends Controller
{

	public function __construct()
	{
		dd(Session::all()); //Return The empty array
	}
	public function index()
	{
	
		echo "<pre>";
		print_r(Session::all()); //It's works
		echo "</pre>"; die;
	}
}
?>

You put below following code in constructor after you can easily get session value in the constructor.

PHP
public function __construct()
{
            $this->middleware(function ($request, $next) {
            if(Session::get("logged_in") == false && empty(Session::get('logged_in'))) {
                Redirect::to('admin/login')->send();
            }
            return $next($request);
        });
}

But you can also some changes and use the following code in protected $middleware = [] on “app/Http/kernel.php” file.

PHP
	/**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        AppHttpMiddlewareCheckForMaintenanceMode::class,
        IlluminateFoundationHttpMiddlewareValidatePostSize::class,
        AppHttpMiddlewareTrimStrings::class,
        IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
        AppHttpMiddlewareTrustProxies::class,
		IlluminateSessionMiddlewareStartSession::class,
		IlluminateViewMiddlewareShareErrorsFromSession::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
            IlluminateSessionMiddlewareStartSession::class,
            // IlluminateSessionMiddlewareAuthenticateSession::class,
            IlluminateViewMiddlewareShareErrorsFromSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
            IlluminateRoutingMiddlewareSubstituteBindings::class,
        ],

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

Please follow and like us:

Hope this code and post will helped you for implement How can get the session value on the controller constructor in the laravel – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  8  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US