Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Session not working in middleware Laravel 5

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());

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.

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 = [
        'IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode',
        'IlluminateCookieMiddlewareEncryptCookies',
        'IlluminateCookieMiddlewareAddQueuedCookiesToResponse',
        'IlluminateSessionMiddlewareStartSession',
        'IlluminateViewMiddlewareShareErrorsFromSession',
        'AppHttpMiddlewareVerifyCsrfToken',
        'AppHttpMiddlewareLanguage',

    ];

So I ask - what am I doing wrong?

Edit: Digging in IlluminateSessionMiddlewareStartSession I found this -

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

1) php artisan make:middleware SetApplicationLanguage

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

IlluminateSessionMiddlewareStartSession::class,
AppHttpMiddlewareSetApplicationLanguage::class,

Notice that this new Middleware comes AFTER the StartSession class.

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

namespace AppHttpMiddleware;

use App;
use Closure;
use IlluminateSupportFacadesAuth;

class SetApplicationLanguage
{

    /**
     * Handle an incoming request.
     *
     * @param IlluminateHttpRequest $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);
    }
}

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...