Laravel 5 - session doesn't work
Laravel 5 handles sessions via a middleware class called StartSession
. More importantly, this middleware is a TerminableMiddleware
and the code that actually saves the data (in your case to the session file) is located in the terminate
method, which is run at the end of the request lifecycle:
public function terminate($request, $response)
{
if ($this->sessionHandled && $this->sessionConfigured() && ! $this->usingCookieSessions())
{
$this->manager->driver()->save();
}
}
When calling dd(Session::get('aa'));
the request is being interrupted before the terminate
method of the middleware can be called.
Funnily enough, the Laravel Middleware Documentation actually explains Terminable Middleware logic by giving the Laravel StartSession
middleware as an example:
For example, the "session" middleware included with Laravel writes the session data to storage after the response has been sent to the browser.
That being said, try using var_dump()
instead of using dd()
.
Why is my Laravel 5.7 session not working?
What's happening here is that when you add this:
session(['test', 'x'])
It adds test at the 0th index of the session and x at 1st.
Now when you again do a
session(['test1', 'xx'])It overwrites the 0th and 1st index of session with new values.
Therefore when you print the session data you get the last values.
You can check this by doing a
dd(session()->all())
and seeing the same on screen.If you want to make a key value relation and store the data in such away, please use the syntax like this:
session(['test' => 'x']);
session(['test1' => 'xx']);
Laravel 5.5: Sessions not working
Based on an answer elsewhere, I had to change the $middlewareGroups
variable to:
'api' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
'throttle:60,1',
'bindings',
],
Laravel 5: Sessions not working the way they should
Where in routes.php
are you setting the session value? It sounds like you're doing something like this:
Session::put('key', 'value');
Route::get('my-route', 'MyController@doSomething');
and then doing this:
class MyController {
public function doSomething()
{
Session::get('key');
}
}
Is that correct? If so, read on...
I'm no expert on the Laravel request lifecycle (for more, see the documentation), but it doesn't surprise me that this doesn't work. The way I think about it is this: the routes.php
file is loaded and executed early in the life cycle - probably first - since it tells the application what code to execute next (ie. what do when a particular request is received). And when I say "early in the life cycle", I mean early - like before sessions are initialized. I believe that the Session::put
call is simply being ignored, since at the time when you're setting the value, the session does not exist.
You may want expand your question with a little more detail about what you're trying to accomplish - there has got to be a better way to do it.
EDIT - in response to the comments below...
I am not saying you should touch the $_SESSION
superglobal - that's a bad idea because I'm not even sure that Laravel uses the native PHP session facility and you have no guarantee that whatever you do will continue to work in the future.
It's not clear what you're trying to do, but to me this sounds like a value that does not belong in the session.
By placing the Session::put
in the routes.php
file, it sounds like you have some value that's important and should be set for every session and every request
If that's the case, and it's a static value, then it's not a session value, it's a configuration value.
If, instead, it's a dynamic value and/or it changes depending on which user is associated with a session, then you can set it in one of several places:
if you're using controller-based routing, you could set this in the controller constructor, although I wouldn't recommend it, because you will probably have to do it for several controllers, leading to code duplication
if you're using closures in your routes, set it there. E.g.
Route::get('some/route', function () {
Session::put('key', 'value');
// this works, because the closure isn't executed until after
// the application is initialized
});you could also do it in middleware
or in a service provider (although I'm not certain that sessions would be available when the service providers are executed).
The best option is probably middleware - this would allow you to set (or calculate) the session value in one place in your code and also associate it with particular routes, if you don't need it for all routes.
Laravel 5.6 - Session is not persisting after it expires
After more investigation, I found out that the issue was caused by barryvdh/laravel-debugbar package. By removing it or turning application into production/debug=false, everything worked as expected. I will update this answer once I have response from barryvdh.
Related Topics
Get Last Word from Url After a Slash in PHP
Laravel 5 How to Get Route Action Name
Http Auth via PHP - PHP_Auth_User Not Set
Patterns for PHP Multi Processes
PHP Class Not Found When Using Namespace
Strpos() with Multiple Needles
Woocommerce: Display Also Product Variation Description on Cart Items
Printing PHP Script Output to File
How Check Memory Location of Variable in PHP
Modify HTML Attribute with PHP
Merge Two Png Images with PHP Gd Library
In PHP, Which Is Faster: Preg_Split or Explode
MySQL Password Hashing Method Old VS New