Skip to content
Advertisement

how to customize Auth::routes in laravel

I hope you are the best. I’m trying to create a multi-language feature in my Laravel app. but I don’t know how to insert Auth::routes in my web routes file to support my multi-language. this code works for me but Auth::routes links don’t set correctly. the app can identify target language but in Auth::routes() links arent’ correctly. for example, all the page is based on target language but links in the local language. (target is en_us but links in fa_IR)

    Route::get('/', function () 
{
   // echo "befpreRpikte".$request->cookie('language').'<br/>';
 //   return ('CookieInRoute'/''.request()->cookie('language'));
     return redirect(App::getLocale().'/welcome');
  
})->middleware(CheckLanguage::class);

Route::get('/{locale}',function($locale)
{
    return redirect($locale.'/welcome');

});

Route::get('{locale}/welcome',function($locale)
{
    //echo('CookieInRoute'/''.request()->cookie('language'));
    //die(App::getLocale());
    return view('welcome');

})->middleware(CheckLanguage::class);

Route::group(['prefix' =>App::getLocale()], function () {

  Auth::routes();
   // Route::get(App::getLocale().'/login','HomeController@login');
//Route::get('{locale}/home','HomeController@index');

});

and my middle ware is:

      public function handle($request, Closure $next)
    {
        //echo(var_dump(request()->cookie('language')));
       //if(empty(request()->cookie('language')))
            cookie()->queue('language',$this->checkUserIsoCode($request->path()),60);
             App::setLocale($this->checkUserIsoCode($request->path()));
       // dd(request()->cookie('language'));
     return $next($request);
    }
    private function checkUserIsoCode($path)
    {
       // echo '<br/> <c> c</c></br>';

     $available_locales=config('app.all_locales');
    
     if($path==null || $path=="/")// => "/" in addressbar
        {
            try
            {
                $userLocale=Location::get(request()->ip())->countryCode;
            }
            catch(Exception $e)
            {

            $userLocale="fa_IR";
            }
        }
         else
         $userLocale=$path[0];//locale  =>/locale/address

     foreach($available_locales as $locale)
     {
        if(strpos($locale,$userLocale)!==false)//if $locale contain userlocale
         {
             $userLocale=$locale;
             break;
         }
     }
     if(!in_array($userLocale,$available_locales,TRUE))
        $userLocale=config('app.fallback_locale');
     return $userLocale;

    }

Advertisement

Answer

You could group all your routes so all of them get locale as prefix:

Route::group(['prefix' => '{locale}'], function() {

    Route::get('/', function () {
        return view('welcome');
    });

    Auth::routes();

    Route::get('/home', 'HomeController@index')->name('home');
})->middleware(CheckLanguage::class);

This blog post seems to solve what you are trying to do:

https://laraveldaily.com/multi-language-routes-and-locales-with-auth/

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement