我正在尝试让我的地区前缀在我所有的路由上工作。
它通常可以工作,但是我遇到了这样的问题:头lang按钮正在从特定的页面中寻找一个参数(我认为)。
标题组件中的标记:
<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>在我的标题组件中。
一旦我尝试根据一个变量打开页面作为路由参数,我就会得到错误:路由-错误消息。
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');
});以及链接到其中一个项目页面的片段:
<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>有办法修复头路由吗?
提前感谢!
菜鸟
发布于 2021-05-25 15:24:44
Route::CurrentRouteName()为您提供了指定的路由,但您缺少参数。
您可以使用request()->route()->parameters获取参数数组。
例如,对于url '/en/architectuur/something',转储request()->route()->parameters返回以下数组。
[
'lang' => 'en',
'project' => 'something'
]然后,只需使用array_merge更改lang键即可。
@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如果您愿意,也可以内联
@foreach (['nl', 'fr', 'en'] as $lang)
<li>
<a href="{{ route(Route::CurrentRouteName(), array_merge(request()->route()->parameters, ['lang' => $lang])) }}">nl</a>
</li>
@endforeachhttps://stackoverflow.com/questions/67690056
复制相似问题