How to Get the Current URL Inside @if Statement (Blade) in Laravel 4?

前端 未结 28 2473
眼角桃花
眼角桃花 2020-11-28 01:15

I am using Laravel 4. I would like to access the current URL inside an @if condition in a view using the Laravel\'s Blade templating engine but I don\'t know ho

相关标签:
28条回答
  • 2020-11-28 01:22

    There are two ways to do that:

    <li{!!(Request::is('your_url')) ? ' class="active"' : '' !!}>
    

    or

    <li @if(Request::is('your_url'))class="active"@endif>
    
    0 讨论(0)
  • 2020-11-28 01:24

    Set this code to applied automatically for each <li> + you need to using HTMLBuilder library in your Laravel project

    <script type="text/javascript">
        $(document).ready(function(){
            $('.list-group a[href="/{{Request::path()}}"]').addClass('active');
        });
    </script>
    
    0 讨论(0)
  • 2020-11-28 01:24

    The simplest way is

    <li class="{{ Request::is('contacts/*') ? 'active' : '' }}">Dashboard</li>
    

    This colud capture the contacts/, contacts/create, contacts/edit...

    0 讨论(0)
  • 2020-11-28 01:26

    A little old but this works in L5:

    <li class="{{ Request::is('mycategory/', '*') ? 'active' : ''}}">
    

    This captures both /mycategory and /mycategory/slug

    0 讨论(0)
  • 2020-11-28 01:27

    A simple navbar with bootstrap can be done as:

        <li class="{{ Request::is('user/profile')? 'active': '' }}">
            <a href="{{ url('user/profile') }}">Profile </a>
        </li>
    
    0 讨论(0)
  • 2020-11-28 01:27

    The simplest way is to use: Request::url();

    But here is a complex way:

    URL::to('/').'/'.Route::getCurrentRoute()->getPath();
    
    0 讨论(0)
提交回复
热议问题