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

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

    For named routes, I use:

    @if(url()->current() == route('routeName')) class="current" @endif
    
    0 讨论(0)
  • 2020-11-28 01:31

    Laravel 5.4

    Global functions

    @if (request()->is('/'))
        <p>Is homepage</p>
    @endif
    
    0 讨论(0)
  • 2020-11-28 01:31

    There are many way to achieve, one from them I use always

     Request::url()
    
    0 讨论(0)
  • 2020-11-28 01:32

    Another way to write if and else in Laravel using path

     <p class="@if(Request::is('path/anotherPath/*')) className @else anotherClassName @endif" >
     </p>
    

    Hope it helps

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

    instead of using the URL::path() to check your current path location, you may want to consider the Route::currentRouteName() so just in case you update your path, you don't need to explore all your pages to update the path name again.

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

    I personally wouldn't try grabbing it inside of the view. I'm not amazing at Laravel, but I would imagine you'd need to send your route to a controller, and then within the controller, pass the variable (via an array) into your view, using something like $url = Request::url();.

    One way of doing it anyway.

    EDIT: Actually look at the method above, probably a better way.

    0 讨论(0)
提交回复
热议问题