【问题标题】:laravel localization using mcamara package giving 404 error on root url but not other urlslaravel 本地化使用 mcamara 包在根 url 上给出 404 错误,但在其他 url 上没有
【发布时间】:2020-08-11 09:30:14
【问题描述】:

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

AppServiceProvider.php 在这个文件中,我通过 https://github.com/stevebauman/location 这个包获取用户 IP 地址,并从数据库中检查语言环境并据此设置语言环境。

        $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 错误。

【问题讨论】:

    标签: php laravel laravel-localization


    【解决方案1】:

    这个步骤对我有用

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

    });

    那么 1 - 删除引导文件夹中的缓存文件 2-php artisan optimize 3-php artisan route:trans:cache 4-php artisan cache:clear 5-php artisan route:clear

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 你在哪里找到的?请提及您的来源。我认为这不太可能是解决方案。
        • 我自己也面临这个问题,这个解决方案对我有用。
        猜你喜欢
        • 1970-01-01
        • 2020-11-26
        • 2021-02-15
        • 2012-11-08
        • 1970-01-01
        • 2020-12-30
        • 1970-01-01
        • 2014-05-19
        • 2011-06-23
        相关资源
        最近更新 更多