The ServiceProvider (register and boot methods) is likely too early in the Request lifecycle; how is the current locale being set?
Get Locale in Laravel problem.
Hi, I am in a situation in which I have a main layout in which then yield multiple views. OK so, this main layout needs some data, and to avoid always getting the same data for the main layout again and again when calling the yielded view, I simply am getting the data for the layout in the ContentServiceProvider.php . There's a problem, when i change the prefix on the rout for the language (fr, en, it ...) and I do app()->getLocale() in the ContentServiceProvider.php it always returns the default one which is set in the config.php even tho everyelse it returns the right locale. What could be the problem?
This is the way in which I change the locale from the front:
{{route(Route::currentRouteName(), 'en')}} {{route(Route::currentRouteName(), 'IT')}} {{route(Route::currentRouteName(), 'de')}} {{route(Route::currentRouteName(), 'fr')}}
@Kris01 how about putting the app()->getLocale() call inside the Closure instead - then it will be evaluated whenever the view is about to render rather than whenever the framework is booting:
view()->composer('front.layout', function($view) {
$view->with([
'contents' => CategoryDifferentLang::whereHas('language',function ($q){
$q->where('slug', app()->getLocale());
})->with('category_relation')->get()
]);
});
Please or to participate in this conversation.