会话在中间件Laravel 5中不起作用 [英] Session not working in middleware Laravel 5

查看:112
本文介绍了会话在中间件Laravel 5中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Laravel 5 Middleware中使用Sessions,但是它们无法正常工作. 具体来说-var_dump(Session::all());在handle方法的开头为我提供了一个值-_tokken的数组,然后在此方法的结尾

Im trying to work with Sessions in Laravel 5 Middleware, but they are not working. To be specific - var_dump(Session::all()); at the start of handle method gives me array with one value - _tokken, then at the end of this method

Session::put('lang',$locale);
var_dump(Session::all());

为我提供了两个值,_tokken和我的lang键,但是刷新后的值相同,据我了解,第二次刷新后应该有相同的结果.

Gives me array with two values, _tokken and my lang key, but after refresh its the same, as I understand there should be same result after second refresh.

虽然我也许在会话中间件之前就已经加载了中间件,这是真的,但是我切换了,现在我的Kernel.php看起来像这样-

I though maybe I have my middleware loaded before Session middleware, which was true, then I switched and now my Kernel.php looks like this -

protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
        'App\Http\Middleware\Language',

    ];

所以我问-我在做什么错了?

So I ask - what am I doing wrong?

在Illuminate \ Session \ Middleware \ StartSession中挖洞,我发现了这一点-

Digging in Illuminate\Session\Middleware\StartSession I found this -

//Note that the Laravel sessions do not make use of PHP "native" sessions in any way since they are crappy.

作为评论,所以我对session_status()的测试并不重要.

as a comment, so my testing with session_status() is not relavent.

推荐答案

我遇到了同样的问题,我在登录时使用Sessions来存储语言环境,然后重定向到主仪表板,但是在加载中间件时,该会话是尚未开始.这样就行不通了.

I had the same problem, I was using Sessions to store the locale at login, then redirect to the main dashboard, but when the middleware loads, the session is not initiated yet. So it wouldn't work.

首先让我说,我不是Laravel专家,但是这种方式在Laravel 5.3中有效:

Let me say first, I'm no Laravel expert, but this way works in Laravel 5.3:

1)php artisan make:middleware SetApplicationLanguage

1) php artisan make:middleware SetApplicationLanguage

2)将此添加到app/Http/Kernel.php $ middlewareGroup变量:

2) Add this to app/Http/Kernel.php $middlewareGroup variable:

\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\SetApplicationLanguage::class,

请注意,此新的中间件位于StartSession类之后.

Notice that this new Middleware comes AFTER the StartSession class.

3)这是我的app/Http/MiddleWare/SetApplicationLanguage.php:

3) This is my app/Http/MiddleWare/SetApplicationLanguage.php:

namespace App\Http\Middleware;

use App;
use Closure;
use Illuminate\Support\Facades\Auth;

class SetApplicationLanguage
{

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request            
     * @param \Closure $next            
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (isset(Auth::user()->locale)) {
            App::setLocale(Auth::user()->locale);
        }

        return $next($request);
    }
}

请注意,在本示例中,我不使用Session.那是因为当我在StartSession类之后添加中间件时,会话可以工作,但是Auth :: user()将再次可用,因此我可以只使用Auth :: user()-> locale,而根本不需要会话.

Notice that I don't use Session in it this example. That's because when I add my Middleware AFTER the StartSession class, session would work, but Auth::user() would be available again, so I could just use Auth::user()->locale and no need for Sessions at all.

但是您可以这样做,只需使用App :: setLocale(Session :: get('locale'))即可,并且原因包括Session外观.

But you could do it, just use App::setLocale(Session::get('locale')) instead and of cause include the Session facade.

这篇关于会话在中间件Laravel 5中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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