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
For named routes, I use:
@if(url()->current() == route('routeName')) class="current" @endif
Laravel 5.4
Global functions
@if (request()->is('/'))
<p>Is homepage</p>
@endif
There are many way to achieve, one from them I use always
Request::url()
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
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.
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.