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

imranniaz's avatar

Initial redirection to login route

Hi,

I am working on laravel 5.2, and my requirement is that I need to pass some parameter to my project URL, http://mywebsite.com/?param=1 which I want to register as a session and then use it before login, but, what happens that laravel redirects to http://mywebsite.com/login and the param is lost and I can't find it in the login function of AuthenticateController

My Question is where i register my session before it gets redirected to /login

Reason: I don't want to show /login?param=1

Thanks

0 likes
7 replies
imranniaz's avatar

Thank you Saeed for quick reply, yes i tried that but some how the handle in middleware is not being called and session variable is not set, even if I comment out all the code in middleware, the login process still gets executed without any error.

ImeDa's avatar

Simply set session variable before calling

$this -> middleware('auth');

in constructor. Not the best way, but should work.

imranniaz's avatar

In laravel 5.2 only this constructor is available which don't set the session

    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

which mean there is some other module getting called before reaching to this constructor.

ImeDa's avatar

Okay... When user comes on http://mywebsite.com/?param=1 this route, there must be a controller handling this request. For example, HomeController. In ur Homecontroller __construct method u can have this:

public function __construct()
    {
    // Put session code here
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }
        

This should work as far as it's laravel 5.2. From laravel 5.3 u can't do it as documentation mentions:

https://laravel.com/docs/5.3/upgrade

Search for Session In The Constructor section.

imranniaz's avatar

Actually for any route under the:

Route::group(['middleware' => ['web'], function(){
    // get('/', 'HomeController@index')
}

The group of middleware 'web' is getting called, and there is no clue how it redirects to login:

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];
ImeDa's avatar

Sorry i was out for a while. What about adding ur custom middleware and add it to the top:

protected $middlewareGroups = [
        'web' => [
        // Custom middleware class here.
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

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

Please or to participate in this conversation.