Consider a navigation menu with a bunch of links and you are trying to activate them based on the current active route name. In normal case you have to return the currentRouteName()
from the controller or maybe in view composer or any other place. I have wrote a simple Laravel composer package that makes it a little bit simpler.
You can take a look it here: https://github.com/tuytoosh/active.
For installation use composer require command like this:
composer require tuytoosh/active
Then use @active()
directive in your blade files.
<a href="#" class="@active('home')">Home page</a>
Without this package you have to write sth like this:
<a href="#" class="@if(Route::getCurrentRouteName() == 'home') active @endif">Home page</a>
After version 1.7.0
You are able to define an array of route patterns like this:
class="@active(['admin.dashboard', 'user.dashboard'])"
If you find it use full give a star :)
Thank you for reading.
Top comments (2)
Funny, I actually did do that with
Route::currentRouteName()
.If this package is mostly adding a blade directive, that can be done here also: laravel.com/docs/9.x/blade#extendi...
I mention this in case anyone is averse to adding a package for something this small.
Thank you for your comment. Yes, actually I have extended a custom directive in this package. I have several purposes for this package.
First I wanted to write a package to be familiar with aspects of package development in Laravel like:
And I hope to make it better and add some other features in future.
And I will be happy to hear your suggestions and maybe contributions :)