I have the following links in the header page:
<li >
<a href="{{ route('root') }}">Home</a>
</li>
<li >
<a href="{{ route('about')}}">About</a>
</li>
<li >
<a href="{{ route('contact') }}">Contact</a>
</li>
And I have the following routes in web.php
Route::get('/',function(){
return redirect(app()->getLocale());
})->name('root');
Route::group(['prefix'=>'{locale}','middleware'=>'setLocale'],function(){
Route::get('/',[FrontEndController::class, 'index'])->name('index');
});
Route::get('/about',function(){
return view('about');
})->name('about');
Route::get('/contact',function(){
return view('contact');
})->name('contact');
the problem is whenever i click a link i go to the index page, in spite of the path in the url is correct.
Note: whenever I remove the Route::group, the links work properly.
please help...
CodePudding user response:
This 'prefix'=>'{locale}' is dynamic, anything you put in the url is going here. You need to rethink about the locale URL.
This will work fine
'prefix'=>'xyz/{locale}'
CodePudding user response:
I solved the problem by adding regular expression for locale:
Route::group(['prefix'=>'{locale}','where' => ['locale' => '[a-zA-Z]{2}'],'middleware'=>'setLocale'],function(){
CodePudding user response:
You need to put all Routes under locale (not only home page), else it will consider that /about
& /contact
on the URLs are locales also, then redirect you to the home page.
For your example, you should do something like :
Route::get('/',function(){
return redirect(app()->getLocale());
});
Route::group(['prefix'=>'{locale}','middleware'=>'setLocale'],function(){
Route::get('/',[FrontEndController::class, 'index'])->name('index');
Route::get('/about',function(){ return view('about');})->name('about');
Route::get('/contact',function(){ return view('contact');})->name('contact');
});
No need to name root
Route, you should use index
for home page.