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

前端 未结 28 2475
眼角桃花
眼角桃花 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:40

    I'd do it this way:

    @if (Request::path() == '/view')
        // code
    @endif
    

    where '/view' is view name in routes.php.

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

    You can use: Request::url() to obtain the current URL, here is an example:

    @if(Request::url() === 'your url here')
        // code
    @endif
    

    Laravel offers a method to find out, whether the URL matches a pattern or not

    if (Request::is('admin/*'))
    {
        // code
    }
    

    Check the related documentation to obtain different request information: http://laravel.com/docs/requests#request-information

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

    This is helped to me for bootstrap active nav class in Laravel 5.2:

    <li class="{{ Request::path() == '/' ? 'active' : '' }}"><a href="/">Home</a></li>
    <li class="{{ Request::path() == 'about' ? 'active' : '' }}"><a href="/about">About</a></li>
    
    0 讨论(0)
  • 2020-11-28 01:43

    For me this works best:

    class="{{url()->current() == route('dashboard') ? 'bg-gray-900 text-white' : 'text-gray-300'}}"
    
    0 讨论(0)
提交回复
热议问题