微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?