Laravel 5.2会话不持久 [英] Laravel 5.2 session not persisting

查看:76
本文介绍了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.

这篇关于Laravel 5.2会话不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆