Setting Bootstrap navbar active class in Laravel 5

前端 未结 13 2042
温柔的废话
温柔的废话 2020-12-23 16:34

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 detaile

相关标签:
13条回答
  • 2020-12-23 16:44

    Set a section on your blade file (let home.blade.php) like

    @section('Home', 'my-active-class')
    

    And set a section on your another blade file (let about.blade.php) like

    @section('About', 'my-active-class')
    

    and yield this section on app.blade.php (Suppose you are extending from app.blade.php)

    ...
    <li class="@yield('Home')"><a href="#">Home</a></li>
    <li class="@yield('About')"><a href="#">About</a></li>
    ...
    
    0 讨论(0)
  • 2020-12-23 16:51

    This is simple: to get your links to be active when using bootstrap, all you need is an if statement inside the class link, for instance: i have my current url as http://example.com/home

     <li class="{{ Request::url() == url('/home') ? 'active' : '' }}"><a href="/home" ></li>
         Home
     </a>
    

    and you are good to go.

    0 讨论(0)
  • 2020-12-23 16:51

    I found the solution:

    composer require devmarketer/easynav
    

    More details : https://github.com/DevMarketer/LaravelEasyNav

    0 讨论(0)
  • 2020-12-23 16:52

    If you are working with named routes. You can use this approach in your views:

    {{ Route::currentRouteNamed('about') ? 'active' : '' }}
    

    or

    {{ Route::is('about') ? 'active' : '' }}
    

    The Illuminate\Routing\Router#is(...) is an alias of the Illuminate\Routing\Router#currentRouteNamed(...).

    0 讨论(0)
  • 2020-12-23 16:57
    <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>
    
    0 讨论(0)
  • 2020-12-23 16:57

    solution is

    <ul class="nav navbar-nav pull-right">
          <li class="{{ Request::is('/') ? 'active' : '' }}">
             <a href="{{ url('/') }}">Home</a>
         </li>
          <li class="{{ Request::is('about') ? 'active' : '' }}">
             <a href="{{ url('/about') }}">About Us</a>
         </li>
          <li class="{{ Request::is('whyus') ? 'active' : '' }}">
             <a href="{{ url('/whyus') }}">Why Us</a>
          </li>
     </ul>
    
    0 讨论(0)
提交回复
热议问题