Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 check if current url matches action

I am currently generating my application urls using {{action('Namespace\Class@method')}}. How would I check if the current page request maps to that current action Namespace\Class@method?

I would like to do something like:

<a href="{{action('Namespace\Class@method')}}
  @if (currentAction('Namespace\Class@method'))
    class="active"
  @endif
>Some link</a>

How would I achieve this in Laravel 5?

like image 735
Ozzy Avatar asked May 24 '15 15:05

Ozzy


1 Answers

This is how I do it without any named routes

<a class="{{ str_contains(request()->url(), '/some-page') ? 'active' : '' }}" href="/some-page">Some Page</a>

This is not a perfect solution but it works for most cases

like image 66
Sajjad Ashraf Avatar answered Oct 06 '22 23:10

Sajjad Ashraf