codezero-be / laravel-localized-routes

⭐️ A convenient way to set up and use localized routes in a Laravel app.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding slug to url while using redirect->back();

luxiflex opened this issue · comments

I wanted to use localization slugs, and i have this controller action at the moment to change the locale.

    public function setLocale($locale){
        session()->put('locale', $locale);
        App::setLocale($locale);

        if (Auth::check()) {
            $user = Auth::user();
            $user->update(['language' => $locale]);
        }
        return redirect()->back();
    }

If i call this method while i'm on example.com i will still get redirected to example.com instead of example.com/locale.

When I click on the next link, everything seems to work fine and adds the slug to the url as expected.

Is there a way to use the redirect->back() and also add the right slug to the url?

Hello,

After some research I found the following problems:

redirect()->back() fetches URL::previous(), which returns the plain text URL in the referer header of the current request (the one where you redirect()->back()).

That plain text URL has a locale slug so that will always update the locale when you redirect to it.

Unfortunately, my code can only convert the locale and parameters of the current route or URL, because it needs a Route object to get the parameters.

There may or may not be a way to do this, but I will need to research this further.

If possible, you could add the User logic to the SetLocale middleware, or create a Store implementation in the Localizer package, if you use that. And then use a locale switcher like this for example:

@if (Route::isLocalized() || Route::isFallback())
    @foreach(Config::get('localized-routes.supported-locales') as $locale)
        @if ( ! App::isLocale($locale))
            <li>
                <a href="{{ Route::localizedUrl($locale) }}">
                    {{ strtoupper($locale) }}
                </a>
            </li>
        @endif
    @endforeach
@endif

Thanks a lot! I've copied the SetLocale middleware and placed the controller logic in there.

  public function handle($request, Closure $next)
  {
      // This package sets a custom route attribute for the locale.
      // If it is present, use this as the locale.
      $locale = $request->route()->getAction('localized-routes-locale')
          ?: $this->getLocaleFromFallbackRoute($request);

      App::make(LocaleHandler::class)->handleLocale($locale);

      //custom logic
      session()->put('locale', $locale);
      App::setLocale($locale);

      if (auth()->check() && auth()->user()->language != $locale) {
          auth()->user()->update(['language' => $locale]);
      }

      return $next($request);
  }

Now to switch language i can simply use

<a class="dropdown-item" href="{{ Route::localizedUrl('en') }}"><i class="flag-icon flag-icon-us"></i> English</a>
<a class="dropdown-item" href="{{ Route::localizedUrl('nl') }}"><i class="flag-icon flag-icon-nl"></i> Nederlands</a>