我有一个不寻常的问题。
我有一个多租户Laravel应用,该应用使用app()-> setLocale()来设置租户的默认语言环境
[当我在视图/控制器中获得任何租户或根应用程序的语言环境时,我将获得一个加密的字符串,如下所示:eyJpdiI6ImtMdFI5Q05KSkU0WlJGaG9HUnF4K2c9PSIsInZhbHVlIjoic3ZVXC90SzVRRjBhUkpQdHJmcHNDUlE9PSIsIm1hYyI6IjE3MDQ4MjBiYTg0YzgwMWRmZmZlMTYxN2EwNDkwODE2MGJmZTNmNDJjOTAwYmE0ZGVlOWY0Yjc4YjIyNmEyMTAifQ==
但是如果我在设置它后立即从AppServiceProvider获得它,我将得到正确的结果,例如:en_AU
我的composer.json:
"require": {
"php": "^7.2",
"appstract/laravel-options": "^2.2",
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.2",
"laravel/passport": "^7.5",
"laravel/telescope": "^2.1",
"laravel/tinker": "^1.0",
"spatie/laravel-activitylog": "^3.9",
"spatie/laravel-query-builder": "^2.3",
"spatie/laravel-schemaless-attributes": "^1.6",
"spatie/laravel-sluggable": "^2.2",
"spatie/laravel-translatable": "^4.2",
"stancl/tenancy": "^2.1"
},
"require-dev": {
"ahuggins/open-on-make": "^0.0.37",
"ajthinking/tinx": "^2.6",
"facade/ignition": "^1.4",
"fzaninotto/faker": "^1.4",
"laravel/homestead": "^9.2",
"laravel/ui": "^1.1",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^8.0"
}
我的AppServiceProvider引导包含以下代码以更改时区和语言环境:
Tenancy::hook('bootstrapped', function ($tenantManager) {
if (Schema::hasTable('options')) {
// Set Tenant Timezone
if (option_exists('timezone')) {
$timezone = option('timezone');
config(['app.timezone' => $timezone]);
date_default_timezone_set($timezone);
}
// Set Tenant Local
logger('Option: ' . option('locale', 'en'));
app()->setLocale(option('locale', 'en'));
logger('App: ' . app()->getLocale());
}
});
Tenancy::hook('ended', function ($tenantManager) {
if (Schema::hasTable('options')) {
// Reset Timezone
$timezone = env('APP_TIMEZONE', 'Australia/Adelaide');
config(['app.timezone' => $timezone]);
date_default_timezone_set($timezone);
//Reset Local
app()->setLocale(env('APP_LOCALE', 'en_AU'));
}
});
我甚至尝试过这样简单的路线:
Route::get('', function () {
return app()->getLocate();
}
仍然没有运气
我也尝试过:
然后,每次用户加载页面时,中间件都会获取此值。