Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 536
user2443854 Avatar asked Jul 11 '13 10:07

user2443854


People also ask

How do I find my URL in blade?

To get the current URL in the Laravel Blade template you can make use of the "url()" helper function which is available globally. This helper can right away be called from the blade template and you can perform conditions based on the value it's returning.

How can you retrieve the full URL for the incoming request in Laravel?

Retrieving the Request URI The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method. To get the full URL, we can use the url method.


2 Answers

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

like image 176
Andreyco Avatar answered Sep 17 '22 06:09

Andreyco


You can also use Route::current()->getName() to check your route name.

Example: routes.php

Route::get('test', ['as'=>'testing', function() {     return View::make('test'); }]); 

View:

@if(Route::current()->getName() == 'testing')     Hello This is testing @endif 
like image 39
Naing Win Avatar answered Sep 17 '22 06:09

Naing Win