繁体   English   中英

laravel 本地化使用 mcamara package 在根 url 上给出 404 错误,但不是其他 url

[英]laravel localization using mcamara package giving 404 error on root url but not other urls

我正在尝试使用 mcamara package 进行语言翻译,但在根 url 上我收到 404 错误。 实际上,我正在尝试检测用户的 ip 地址,然后根据该国家/地区设置区域设置。 我将语言环境和国家/地区名称存储在数据库中。 下面是我的代码:

AppServiceProvider.php in this file i am fetching user ip address through https://github.com/stevebauman/location this package and checking the locale from database and set the locale according to that.

        $ip = request()->getClientIp(); 
        $position = Location::get($ip); 
        $locale = Locale::where('country_code',strtolower($position->countryCode))->first();
        if($locale){
            LaravelLocalization::setLocale($locale->country_code);
            
        }

Web.php

Route::group([
'prefix' => LaravelLocalization::getCurrentLocale(),
'middleware' => ['localizationRedirect', 'localeViewPath' ]], function(){
   Route::get('/',function(){
     dd('check');
  }); 
}

我收到 404 错误。

这个步骤对我有用

Route::group(['prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]],function (){
// routes here

});

then 1 - remove cache files in bootstrap folder 2- php artisan optimize 3- php artisan route:trans:cache 4- php artisan cache:clear 5- php artisan route:clear

https://github.com/mcamara/laravel-localization/blob/master/CACHING.md

这个 url 对你有帮助。 use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes; RouteServiceProvider中的 trait 这个问题在route:trans:cache而不是route:cache

go 到 bootstrap 文件夹并删除 (config.php) 文件,如果有任何名为 (route.php) 的文件也从 bootstrap 文件夹中删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM