session issue

Published 1 week ago by nnjeim

I am using Laravel 5.4 and i am setting the session at the controller level.

A selector change in blade submits the new value to be set in the session.

following the first request the session variable is not being overritten..

what i am missing here? any hint?

$request->session()->put('company_id', $company_id);

Talinon
Talinon
1 week ago (39,120 XP)

When you say that it submits from a change in blade, do you mean an AJAX request?

ejdelmonico

Are you using barryvdh/laravel-debugbar? It shows the sessions values.

nnjeim
nnjeim
1 week ago (1,880 XP)

Hi and thanks for your replies.

On selector value change I am submitting a form from blade to a route controller.

from JS event.preventDefault(); document.getElementById('set-company-id').submit();

the controller method is as below $request->session()->put('company_id', $company_id); return back();

i am printing in the blade view {{ session('company_id') }}

On the first selector change the value is set to 1 for example then on subsequent changes it remains as 1.

I tried to return the value of company_id from the controller and it was returning correctly.

So i came to the conclusion that the problem is in the session setting.

I also tried to change the session driver from database back to file.... the result remained the same.

robrogers3

i don't understand this:

event.preventDefault(); document.getElementById('set-company-id').submit();

how does the element with id: set-company-id, send the proper value to be updated in the session.

document.getElementById('set-company-id') whether it's a form or what not, means it must be unique. If you have multiple elements with the same id, you'll always be sending the same value. (I think it's the first one in the DOM).

am I missing something?

nnjeim
nnjeim
1 week ago (1,880 XP)

Hi robrogers3,

in the submitted for there is a named select element, the select action submits the form to a route controller. I already validated that the selected value is submitted correctly by returning it to the view. It is clear that the problem resides in the session setting. I have read somewhere that this problem is related to scoped routes. Any suggestion here?

Talinon
Talinon
1 week ago (39,120 XP)

Have you tried calling Session::save() after the put()? It wouldn't answer the question "why" this is happening, but it would be interesting if that did persist the session value.

nnjeim
nnjeim
1 week ago (1,880 XP)

Hi Talinon,

i have added Session::save() after the put, this does not seem to change the problem. i have returned the session('company_id') from the controller and it is returning correctly when i toggle between different companies.

I discovered that the issue is that the session is returning null in other controllers, models or blade views.

anyone ever encountered something similar?

robrogers3

I got a question what middleware are you applying to the different route groups?

do they both include the web middleware?

if they don't then thats your problem.

nnjeim
nnjeim
1 week ago (1,880 XP)

Hi robrogers3,

i am using scoped controllers. But all are part of web.php

i am using Auth as the group middleware but i am not explicitly using web...

my understanding is that web middleware is applied by default to web.php

i might be mistaken.

app\Http\Company\CompanyController is where the session is set.

public function setCompany(Request $request) {

       $company_id = $request->input('company_id');

        $request->session()->forget('company_id');

        $request->session()->put('company_id', $company_id);

        return back();
    }

but i am trying to use the session setting in blade and in other controllers sessions. other controllers are in different scopes.

in addition i think i need to mention that i am using the database driver, but i also reverted back to the file driver and it didn't fix the problem.

what i am sure of is that in the CompanyController the session is being set correctly because i responsed with session('company_id') and the response was correct.

but calling the session from elsewhere is returning null in the model. sometimes it set it to a value but cannot overwrite.

as u can notice up in the code i am also applying forget to make sure the session attribute is flashed but still this is not helping.

Thanks in advance for the patience you are talking to read all this.

Talinon
Talinon
1 week ago (39,120 XP)

To add to what robrogers3 mentioned, you can try this command to confirm if the web middleware is being applied to your route:

php artisan route:list
nnjeim
nnjeim
1 week ago (1,880 XP)

Hi Talinon,

i have verified all group routes are being attributed Auth and Admin (copy of web) in kernel... the routes are being used in a prefixed group ('admin'). i am attaching here below the kernel definition

'admin' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

i am doubting that the issue is related to prefixed routes...

Talinon
Talinon
1 week ago (39,120 XP)

The fact it works in your CompanyController, but none of the other controllers, suggests to me that it is persisting multiple sessions.

Since you reverted back to your file driver for sessions, try deleting everything in your storage/framework/sessions directory.

Login normally, then hit your CompanyController to see if an additional file is created.

nnjeim
nnjeim
1 week ago (1,880 XP)

Hi Talinon,

I did as indicated.

Your doubt was right, no file is created in the sessions directory.

Please sign in or create an account to participate in this conversation.