Laravel localize "setlocale(): Argument #1 ($category) must be of type int, Illuminate\Http\Request given" error

huangapple go评论65阅读模式
英文:

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 installphp 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 = [
    &#39;web&#39; =&gt; [
        \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**, // &lt;- **locale class is here** 
    ],

    &#39;api&#39; =&gt; [
        // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
        \Illuminate\Routing\Middleware\ThrottleRequests::class.&#39;:api&#39;,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];
protected $middlewareAliases = [
    &#39;auth&#39; =&gt; \App\Http\Middleware\Authenticate::class,
    &#39;auth.basic&#39; =&gt; \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    &#39;auth.session&#39; =&gt; \Illuminate\Session\Middleware\AuthenticateSession::class,
    &#39;cache.headers&#39; =&gt; \Illuminate\Http\Middleware\SetCacheHeaders::class,
    &#39;can&#39; =&gt; \Illuminate\Auth\Middleware\Authorize::class,
    &#39;guest&#39; =&gt; \App\Http\Middleware\RedirectIfAuthenticated::class,
    &#39;password.confirm&#39; =&gt; \Illuminate\Auth\Middleware\RequirePassword::class,
    &#39;signed&#39; =&gt; \App\Http\Middleware\ValidateSignature::class,
    &#39;throttle&#39; =&gt; \Illuminate\Routing\Middleware\ThrottleRequests::class,
    &#39;verified&#39; =&gt; \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    **&#39;setlocale&#39; =&gt; \App\Http\Middleware\SetLocale::class,**
];

/app/Http/Middleware/SetLocale.php

class SetLocale {
    public function handle(Request $request, Closure $next)
    {
        if (Session::has(&#39;locale&#39;)) {
            App::setLocale(Session::get(&#39;locale&#39;));
        }
        return $next($request);
    }   
}

/routes/web.php

Route::get(&#39;/&#39;, function () {
    return redirect(app()-&gt;getLocale());
})-&gt;name(&#39;home&#39;);
Route::get(&#39;/setlang&#39;, [Controller::class, &#39;changeLanguage&#39;])-&gt;name(&#39;setlang&#39;);
Route::group(
[
    &#39;prefix&#39; =&gt; &#39;{locale}&#39;,
    &#39;where&#39; =&gt; [&#39;locale&#39; =&gt; &#39;[a-zA-Z]{2}&#39;],
    &#39;middleware&#39; =&gt; &#39;setlocale&#39;
],
function () {
    Route::get(&#39;/&#39;, [Controller::class, &#39;homeview&#39;]);
});

答案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;

huangapple
  • 本文由 发表于 2023年6月22日 14:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定