Header.blade 组件区域设置开关 - LARAVEL 8如何?

问题描述

我正在尝试让我的语言环境前缀适用于我的所有路由。

它通常有效,但我遇到了这个问题,其中标题语言按钮正在寻找来自特定页面的参数(我认为)。

来自我的标题组件的标签

<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>

在我的标题组件中。

一旦我尝试打开基于变量作为路由参数的页面,我就会收到错误消息: 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');
});

以及来自这些项目页面之一的链接的片段:

<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>

有没有办法修复头部路由?

提前致谢!
一个菜鸟

解决方法

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','en'] as $lang)
  <li>
    <a href="{{ route(Route::CurrentRouteName(),array_merge(request()->route()->parameters,['lang' => $lang])) }}">nl</a>
  </li>
@endforeach

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...