I am working on developing a blog with localization in different languages. One of the tutorials did this with the help of an intermediary - middleware. Everything works, but there are a few problems.
Firstly, when changing the language, each time it is transferred to the main page, but I would like to stay on the same one.
Secondly, the main page looks like this: localost: 8000 / en, because English is the default language. But I'm not sure that this is good for seo. If it is English, then the idea should be localost: 8000. Tried to solve it through sessions, but sessions did not memorize variables. Here is the code that is at the moment:

<?php namespace App\Http\Middleware; use Closure; use Illuminate\Routing\Redirector; use Illuminate\Http\Request; use Illuminate\Foundation\Application; use Illuminate\Contracts\Routing\Middleware; use Illuminate\Support\Facades\URL; class Language implements Middleware { public function __construct(Application $app, Redirector $redirector, Request $request) { $this->app = $app; $this->redirector = $redirector; $this->request = $request; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { // Make sure the current local exists $locale = $request->segment(1); // If the locale is added to to skip_locales array continue without locale if (in_array($locale, $this->app->config->get('app.skip_locales'))) { return $next($request); } else { // If the locale does not exist in the locales array continue with the fallback_locale if (!array_key_exists($locale, $this->app->config->get('app.locales'))) { $segments = $request->segments(); array_unshift($segments, $this->app->config->get('app.fallback_locale')); return $this->redirector->to(implode('/', $segments)); } } if (!$locale) $locale = $this->app->config->get('app.fallback_locale'); $this->app->setLocale($locale); return $next($request); } 

How can you solve these problems?

    2 answers 2

    The solution to your problems. https://habrahabr.ru/post/264435/

    The selected language is stored in sessions. By default, the language that is set in the configs is taken. Switching occurs by moving to the page from which the user redirects back.

        <?php namespace App\Http\Middleware; class Language implements Middleware { public function __construct(Application $app, Request $request) { $this->app = $app; $this->request = $request; } public function handle($request, Closure $next) { $fallback_locale = $this->app->config->get('app.fallback_locale'); $locale = $request->segment(1, $fallback_locale); if (in_array($locale, $this->app->config->get('app.skip_locales'))) { return $next($request); } else { if (!array_key_exists($locale, $this->app->config->get('app.locales'))) { $segments = $request->segments(); array_unshift($segments, $fallback_locale); return redirect()->back(); } } $this->app->setLocale($locale); return $next($request); } }