简体   繁体   中英

How to use session for all controllers in Laravel 5.6

I have seen some posts, like
Laravel5 - can't get Sessions working
Laravel 5 - session doesn't work

But I didn't get a solution for what I want.

I'm trying to build a multi language site. I use session to store this.
I know that in my controller, like ProductController, function index(){}, I can do like this

$this->request->session()->put('locale', 'en');
$this->request->session()->keep('locale');
$value = $this->request->session()->get('locale');
dd($value);
dd(Session::all());

Session::put('locale', 'en');
Session::keep('locale');
Session::save();
$value = $this->request->session()->get('locale');
dd($value);
dd(Session::all());

But I want to use in Controller.php, so I only need to do the session set and get only once, not in every controller I created.

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Session;

class Controller extends BaseController
{
  use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

  public function __construct()
  {
    Session::put('locale', 'zh');
    Session::save('locale');
    $value = Session::get('locale');
    dd($value);
    dd(Session::all());
  }
}

But this doesn't work. Or I should do this in some place, some after middleware, I'm not familiar with this. Can someone give me some suggestions?

You can use a middleware and register it in your kernel.php file in the web index. It will be call for each web request.

So if you don't want to call it for each request, just add the middleware on concerned routes with Route::group for example and register it in kernel.php in $routeMiddleware .

I tried any solution I could find. Session doesn't work in middleware because the Session has not started yet. You'll always get null at this point.

There is a way and a catch. This how it works in my case for an ArticleController (L5.6)

public function __construct()
{
    $this->middleware(function ($request, $next)
    {
       $locale = config('app.fallback_locale');
        if (session('locale'))
        {
            $locale = session('locale');
        }
        if (\Request::has('lang')) 
        {
            $locale = \Request::get('lang');
            session(['locale' => $locale]);
        }
       \App::setLocale($locale);

       return $next($request);
    });
}

This does do the trick. BUT. Only after you run php artisan config:clear or config:cache . \\Request::get('lang') uses ?lang=de in the route.

Keep in mind that the language stays German until ?lang=en comes by. This is useful for me because only parts of the page get translated. It is not SEO friendly.

The reason why this works and what I understood is the __construct in Controllers are cached. I'm not sure how that exactly works.

Check this working example where you see 'Artikel in Deutsch'. From there on you stay in German till you search in English or click on 'English Articles'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM