英文:
Laravel localize "setlocale(): Argument #1 ($category) must be of type int, Illuminate\Http\Request given" error
问题
I developed multi language web site in Laravel 10. Everything was working fine, since composer does not support laravel 10 libraries on my server, I created a laravel 9 project and moved only the following folders;
我在Laravel 10中开发了多语言网站。一切都运行正常,但由于Composer在我的服务器上不支持Laravel 10的库,所以我创建了一个Laravel 9项目,并只移动了以下文件夹:
/app
/public
/resources
/routes
/composer.json
then I did composer install and php artisan optimize, the result was successful. But the problem is; When I access my website, I get the following error:
然后我运行了composer install
和php artisan optimize
,结果是成功的。但问题是,当我访问我的网站时,我收到以下错误:
setlocale(): Argument #1 ($category) must be of type int, Illuminate\Http\Request given
/app/Http/Kernel.php :
/app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\SetLocale::class, // <- locale class is here
],
'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
protected $middlewareAliases = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'setlocale' => \App\Http\Middleware\SetLocale::class,
];
/app/Http/Middleware/SetLocale.php
/app/Http/Middleware/SetLocale.php
class SetLocale {
public function handle(Request $request, Closure $next)
{
if (Session::has('locale')) {
App::setLocale(Session::get('locale'));
}
return $next($request);
}
}
/routes/web.php
/routes/web.php
Route::get('/', function () {
return redirect(app()->getLocale());
})->name('home');
Route::get('/setlang', [Controller::class, 'changeLanguage'])->name('setlang');
Route::group(
[
'prefix' => '{locale}',
'where' => ['locale' => '[a-zA-Z]{2}'],
'middleware' => 'setlocale'
],
function () {
Route::get('/', [Controller::class, 'homeview']);
});
英文:
I developed multi language web site in Laravel 10. Everything was working fine, since composer does not support laravel 10 libraries on my server, I created a laravel 9 project and moved only the following folders;
/app
/public
/resources
/routes
/composer.json
then I did composer install and php artisan optimize, the result was successful. But the problem is; When I access my website, I get the following error:
setlocale(): Argument #1 ($category) must be of type int, Illuminate\Http\Request given
/app/Http/Kernel.php :
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
**\App\Http\Middleware\SetLocale::class**, // <- **locale class is here**
],
'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
protected $middlewareAliases = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
**'setlocale' => \App\Http\Middleware\SetLocale::class,**
];
/app/Http/Middleware/SetLocale.php
class SetLocale {
public function handle(Request $request, Closure $next)
{
if (Session::has('locale')) {
App::setLocale(Session::get('locale'));
}
return $next($request);
}
}
/routes/web.php
Route::get('/', function () {
return redirect(app()->getLocale());
})->name('home');
Route::get('/setlang', [Controller::class, 'changeLanguage'])->name('setlang');
Route::group(
[
'prefix' => '{locale}',
'where' => ['locale' => '[a-zA-Z]{2}'],
'middleware' => 'setlocale'
],
function () {
Route::get('/', [Controller::class, 'homeview']);
});
答案1
得分: 1
你遇到的错误与 PHP 中的 setlocale()
函数有关。setlocale()
函数用于设置应用程序的不同方面的区域设置信息,如日期和时间格式化、数字格式化和字符串排序。
要解决此问题,您需要对 /app/Http/Kernel.php
文件中的中间件别名进行更改。
请确保在 SetLocale
中间件文件的顶部拥有以下导入语句:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
英文:
The error you're encountering is related to the setlocale()
function in PHP. The setlocale()
function is used to set the locale information for different aspects of your application, such as date and time formatting, number formatting, and string collation.
To fix this issue, you need to make changes to the middleware aliases in the /app/Http/Kernel.php
file.
Please ensure that you have the following import statements at the top of the SetLocale
middleware file:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论