Laravel 5.2会话不持久
最近我一直在Laravel 5.2中从事一个项目,现在我遇到了会话无法持久的问题.我已经阅读了有关此问题的大多数问题,但是每个人都有与我已经尝试过的相同答案-应用Web中间件.
Lately I've been working on a project in Laravel 5.2 and now I'm having problems with sessions not persisting. I've read most of the questions already asked regarding this but everyone has the same answer that I have already tried - applying web middleware.
我已经阅读到有一个新的L5.2更新,其中默认情况下已经应用了Web中间件组.我用php artisan route:list
检查了路由,发现每条路由仅应用了1个Web中间件.
I've read that there was a new L5.2 update where the web middleware group is already applied by default. I checked my routes with php artisan route:list
and I can see that every route has only 1 web middleware applied.
我正在使用$request->session()->put('key', 'value')
创建会话,但是一旦我注释了这一行,就再也看不到会话了.
I'm creating session with $request->session()->put('key', 'value')
but as soon as I comment that line the session is nowhere to be seen anymore.
修改
当我访问新闻页面时,我想在控制器内设置会话,但是我也在一条简单的测试路线上进行了尝试.我将其设置为news/{id}
的路线,我想在/
I want to set the session inside a controller when I visit a news page, but I tried it on a simple test route as well. Route where I set this is news/{id}
and I want to use it on the front page which is in /
我希望在会话中存储最近访问过的页面,这样我就可以在首页上将其显示给用户.
I wish to store recently visited pages in session so I can then show it to the user on the front page.
我保持不变的会话配置文件.所以它正在使用文件驱动程序
Session config file I left untouched. So it's using file driver
以下是经过测试的可用于您的项目的路线 请使用中间件代替路由文件中的功能
Here is a tested routes to use for your projects Please use a middleware instead of the function in the routes file
routes.php
routes.php
// Only as a demo
// Use a middleware instead
function addToSession ($routeName) {
$visited = session()->get('visited', []);
array_push($visited, $routeName);
session()->put('visited', $visited);
}
Route::get('/', function () {
addToSession('/');
return view('welcome');
});
Route::get('/second', function () {
addToSession('/second');
return view('welcome');
});
Route::get('/third', function () {
addToSession('/third');
return view('welcome');
});
Route::get('/history', function() {
return session()->get('visited');
});
/history路由将返回具有历史记录的JSON.
The /history route will return a JSON having the history.