I am trying to get my locale prefix to work on all my routings.
It generally works but I get this issue where the header lang buttons are looking for a parameter from a specific page (I think).
Tags from my header component:
<li><a href="{{ route(Route::CurrentRouteName(), 'nl') }}">nl</a></li>
<li><a href="{{ route(Route::CurrentRouteName(), 'fr') }}">fr</a></li>
<li><a href="{{ route(Route::CurrentRouteName(), 'en') }}">en</a></li>
In my header component.
Once I try to open a page based on a variable as route-parameter I get the error: routing-error-message
Web.php
Route::redirect('/', '/nl');
Route::prefix('/{locale}')->group(function () {
Route::get('/', 'App\Http\Controllers\Controller@showLanding')->name('home');
Route::get('intenties-van-de-site', 'App\Http\Controllers\IntentionsController@showIntentionsSite')->name('intenties-van-de-site');
Route::get('intenties-bij-een-ontwerp', 'App\Http\Controllers\IntentionsController@showIntentionsProject')->name('intenties-bij-een-ontwerp');
Route::get('architectuur', 'App\Http\Controllers\ArchitectureController@showArchitecture')->name('architectuur');
Route::get('architectuur/{project}', 'App\Http\Controllers\ArchitectureController@showProject')->name('project-title');
});
And a snippet from a link to one of those project pages:
<a href="{{ route('project-title', ['project' => '1978-reel-boom', 'locale' => app()->getLocale() ]) }}">
<img alt="1978 Reel Boom" title="1978 Reel Boom" src="{img url}">
<p>1978 Reel Boom</p>
</a>
Is there a way to fix the header routing?
Thanks in advance!
A rookie
Route::CurrentRouteName()
gives you the named route, but you're missing parameters.
You can get the parameters array using request()->route()->parameters
.
For example, for the url '/en/architectuur/something'
, dumping request()->route()->parameters
returns the following array.
[
'lang' => 'en',
'project' => 'something'
]
Then, you'd only need to change the lang key using array_merge
.
@foreach (['nl', 'fr', 'en'] as $lang)
@php($params = array_merge(request()->route()->parameters, ['lang' => $lang]))
<li>
<a href="{{ route(Route::CurrentRouteName(), $params) }}">nl</a>
</li>
@endforeach
Or inline if you prefer
@foreach (['nl', 'fr', 'en'] as $lang)
<li>
<a href="{{ route(Route::CurrentRouteName(), array_merge(request()->route()->parameters, ['lang' => $lang])) }}">nl</a>
</li>
@endforeach