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?