@kickthemooon - https://laravel.com/docs/5.2/session#introduction good read will put you in no time there.
Jan 11, 2016
6
Level 5
Set session variable at runtime
I need to set a session variable anytime a visitor visits my site. I am not sure where to set that session variable?
Where can I set a session variable at app runtime?
Level 5
I solved it at the middleware, wasnt aware that my colleague set up a language middleware so the middleware looks like this now:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
class Language
{
public function handle($request, Closure $next)
{
if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) {
App::setLocale(Session::get('applocale'));
} else { // This is optional as Laravel will automatically set the fallback language if there is none specified
App::setLocale(Config::get('app.fallback_locale'));
Session::set('applocale', Config::get('app.fallback_locale'));
}
return $next($request);
}
}
I am setting the applocale variable from the app.fallback_locale
and I am including the middleware in the kernel web middleware group:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\App\Http\Middleware\Language::class,
],
'api' => [
'throttle:60,1',
],
];
Please or to participate in this conversation.