skip to Main Content

I have a Laravel 5.4 app where I set the Locale of the app in a middleware, depending on the root domain, because I have a domain for each language. This is because of SEO & Adwords stuff, I can’t change that.

Secondly I have a ViewComposerServiceProvider where I prepare Data for the Layout View. This data depends on the Language, but the language hasn’t been set yet, because the ServiceProvider is executed first.

What should I do?

2

Answers


  1. Chosen as BEST ANSWER

    I solved it the way, that I made a GlobalVarServiceProvider that gets the language and provides it in the request

    $lang = Cache::rememberForever("aGreatKey", function () use ($stripped_domain) {
                    return Lang::where('domain', $stripped_domain)->first();
                });
    $request->attributes->add(['language' => $lang]);
    

    Then I can get it in the ViewComposerServiceProvider (and everywhere else)

    $this->sprache = $request->attributes->get('sprache');
    

    Important is, that the GlobalVarServiceProvider is called before the ViewComposerServiceProvider.


  2. If the current workflow is invalid, you should change it.

    Probably you should move choosing language into provider (same or other) to have language already set in ViewComposerServiceProvider

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search