343

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 how to do it.

I know that it can be done using something like <?php echo URL::current(); ?> but It's not possible inside an @if blade statement.

Any suggestions?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
user2443854
  • 3,439
  • 2
  • 13
  • 3

30 Answers30

424

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

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
Andreyco
  • 22,476
  • 5
  • 61
  • 65
  • 3
    `url()` [now returns a builder instance](https://laravel.com/docs/5.4/helpers#method-url). must use `url()->current()` – Jeff Puckett Mar 14 '17 at 18:45
  • @JeffPuckettII Could you provide Laravel version this change is related to? I will add note about this to answer. TY. – Andreyco Mar 15 '17 at 11:47
  • I'm not sure when it started, but the link I provided is for current 5.4 – Jeff Puckett Mar 15 '17 at 12:22
  • 1
    Although I upvoted this a long time ago, today I was having trouble with it on Laravel 5.7 and eventually got this to work instead: `@if (Request::path() != 'login')`. Notice the lack of slash before "login", too. – Ryan Feb 19 '19 at 20:58
  • This leaves off the query string. Some answers below recommended `url()->full()`, which includes the query string. – Skeets Feb 06 '23 at 02:30