当 url 路径在 Laravel 6.2 中发生变化时,将 class 添加到 header

Add a class to header when url path changes in Laravel 6.2

如何在导航到各个博客页面时添加 class。

例如,当用户访问主页时,header 颜色应为金色,当用户导航至每个博客时,header 背景颜色应为红色。

我是这样做的,每次添加新博客页面时,我都需要将 url path 添加到 header。

<div id="header" class="headertop {{ request()->is('blog','blog/test-one','blog/test-two','blog/test-three') ? 'blog-bg-red' : '' }}" >
<nav></nav>
</div>

有什么方法可以在 url 通过 blog/ 时添加 class 吗?

我认为更好的方法是像下面这样从控制器传递一个变量:

return view('greeting')->with('name', 'Victoria');

在视图中您可以检查 {{ $name }} 的值。

我认为这要好得多,因为您不需要依赖 url。

request->is() 使用 Str::is() 所以你可以像这样:

<div id="header" class="headertop {{ request()->is('blog', 'blog/*') ? 'blog-bg-red' : '' }}" >
    <nav></nav>
</div>

或者,如果只有索引页是金色的,其余的是红色的,您可以这样做:

<div id="header" class="headertop {{ request()->is('/') ? '' : 'blog-bg-red' }}" >
    <nav></nav>
</div>