I've been wondering around looking for solutions, but can't really understand especially when creating helpers. I'm new in Laravel and I want a simple or if not a detailed instruction on how to set the active class for my bootstrap navbar.

以下是我迄今为止所做的,但无法完成的事情:

<div class="header clearfix">
        <nav>
          <ul class="nav nav-pills pull-right">
            <li class=""><a href="{{ url('/') }}">Home</a>
            </li>
            <li {{ Request::is('about*') ? ' class="active"' : null }}><a href="{{ url('about') }}">About Us</a>
            </li>
            <li><a href="{{ url('auth/login') }}">Login</a>
            </li>
        </ul>
    </nav>
    <h2 class="">Tobacco Prevention and Control Program</h2>
</div>

EDIT

Setting class="active" will make all nav-pills active. The intended effect is that only the li of the current page have the active class.

推荐答案

<ul class="nav nav-second-level">
                    <li class="{{ Request::segment(1) === 'programs' ? 'active' : null }}">
                        <a href="{{ url('programs' )}}" ></i> Programs</a>
                    </li>
                    <li class="{{ Request::segment(1) === 'beneficiaries' ? 'active' : null }}">
                        <a href="{{url('beneficiaries')}}"> Beneficiaries</a>
                    </li>
                    <li class="{{ Request::segment(1) === 'indicators' ? 'active' : null }}">
                        <a href="{{url('indicators')}}"> Indicators</a>
                    </li>                     
                </ul>

Laravel相关问答推荐

Laravel中的HTTP请求如何工作?

如何在 Laravel 9 中获取带分页的订单列表?

将错误消息抛出并显示为关联数组

使用Laravel Blade Formatter和PHP Intelephense进行 VSCode 格式化

Laravel Blade - 产生内部部分

Laravel 6 Passport 在错误的凭证上返回 400 Bad request

插入重复键时,laravel eloquent 忽略错误

在 laravel 的集合中找到?例子

实施 Payum/Laravel 定期付款

如何在 Laravel 5.5 的 FormRequest 类中返回自定义响应?

Laravel 5.4 LengthAwarePaginator

如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度?

Web 服务器如何处理请求?

Laravel 5:在同一字符串上使用 bcrypt 给出不同的值

Composer 致命错误:声明 Fxp... 必须与第 334 行的 ...AbstractAssetsRepository.php 兼容

Guzzle 返回流空体而不是 json 体

在 laravel 的测试套件中只运行一个单元测试

如何在 Laravel 5.2 中使用 OR 条件将多个参数传递给中间件

Laravel 仓库

找不到 HOME 环境 -- 扩展 `~'