If you try to "put" or change session data in a middleware it should update the session, and it should work this way on every request not just the first.
Currently it appears if you set session data in the middleware it will work the first time the code is run, but running the same code again subsequent times in the same session the middleware's alterations of the session will not persist. (been testing with file as the cache driver, though this happening on our server with redis too.)
I've been trying to create a middleware in a plugin in October CMS, that takes from the input a value and stores it in session to be periodically displayed in a template.
Middleware function:
public function handle($request, Closure $next)
{
session()->put('foo', input('foo'));
logger('StartSession: foo: ', [session('foo')]);
return $next($request);
}
Registering middleware in plugin.php
public function register()
{
$this->app->make('Illuminate\Contracts\Http\Kernel')->prependMiddleware('October\Demo\Middleware\StartSession');
}
Method in plugin for accessing session
public function registerMarkupTags()
{
return [
'functions' => [
'session' => [Session::class, 'get']
]
];
}
Usage in demo theme
<h1>{{ session('foo') }}</h1>
This works on the first run. If I put foo as query string foo is displayed on the page. However on a second run if I changed the query string to bar, foo persists on the page.
Here is an example of the issue in a fresh installed October Instance
https://github.com/reed-josh/october-session-issue
v1.0.443
@w20k do you want to look into this?
@LukeTowers , sounds fun, will take a look, but after weekends, I guess ;) Father is celebrating his jubilee (60th birth anniversary).
Found the issue, but still looking for cause.
Every time you refresh the page, a new session object will be created. Easy to check, add 'Session:save()' after a 'session()->put('test', 'test')'.
public function handle($request, Closure $next)
{
session()->put('foo', input('foo'));
session()-save(); //Session:save();
logger('StartSession: foo: ', [session('foo')]);
return $next($request);
}
The question is cleared, if you want to use session, you have to process request first, and then your session object will be accessible.
And it's not a bug, but a proper behaviour of Laravel Stack.
public function handle($request, Closure $next)
{
// first you process the request and save the response, so your controller is
// executed before the middleware and the session i populated
$response = $next($request);
// now you can access the values shared on this very request
$store = session();
$value = input('foo');
$var = $store->get('foo');
$store->put('foo', $value);
logger('StartSession: foo: ', [session('foo')]);
return $response; // don't forget to return the response to the user
}
@reed-josh does @w20k's answer help?
So in a fresh install of Laravel 5.7 I am able to create the behavior that I want.
Middleware
public function handle($request, Closure $next, $guard = null)
{
session()->put('foo', request()->get('foo'));
$response = $next($request);
return $response;
}
and in the welcome.blade.php file
<div class="title m-b-md">
Laravel!
<?php dd(session()->get('foo')) ?>
</div>
That is consistently returning input value of foo as expected.
Upon some experimentation it does work this way in October CMS as well if I dump the session before the twig template. Like dumping and exiting the script in the OnStart() function for example.
It's as if I allow the twig template code to run the session in my file freezes and won't update.
@reed-josh Might take a while, need to test this behaviour with Laravel also. Or if you could help me out, and post your code on github? :) Would be perfect!
Hey, sorry I haven't been on for a few days. Yes, I would be happy to post it tomorrow, left the code on my work computer.
Hey, sorry this took me so long. Crazy week.
Here is the example of it working as I described it in Laravel.
@reed-josh , thanks a lot!
@w20k no problem. Is that enough for you to pinpoint the issue?
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days. If this issue is still relevant or you would like to see action on it, please respond and we will get the ball rolling.
@reed-josh is this still an issue?
Most helpful comment
@LukeTowers , sounds fun, will take a look, but after weekends, I guess ;) Father is celebrating his jubilee (60th birth anniversary).