简体   繁体   中英

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

i am trying to use mcamara package for language translation but on the root url i am getting 404 error. Actually i am trying to detect the ip address of the user and then set the locale according to that country. i stored the locales and country names in the database. Below is my code:

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');
  }); 
}

i am getting 404 error.

this steps works for me

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

this url help to you. use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes; trait in RouteServiceProvider this problem at route:trans:cache instead of route:cache

go to bootstrap folder and delete (config.php) file and if there is any file called (route.php) delete it too from bootstrap folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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