Skip to content
Laravel helper for recognising the current route, controller and action.
Branch: master
Clone or download
dwightwatson Merge pull request #19 from tabuna/master
 Added support laravel v6.0
Latest commit 9d19820 Sep 3, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Added support laravel v6.0 Sep 2, 2019
tests Test enhancement Mar 23, 2019
.gitignore added ability to define a fallback class Sep 6, 2018
.travis.yml Test enhancement Mar 23, 2019
LICENSE.txt Initial commit Mar 6, 2014 updated doc Sep 6, 2018
composer.json Added support laravel v6.0 Sep 2, 2019
phpunit.xml Bump to PHP 7+/Laravel 5.5+ Sep 7, 2018

Active for Laravel

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

Active is a helper package for Laravel that makes it easy to recognize the current path or route, useful for adding 'active' classes (like those used in the Boostrap framework) and performing other actions only when a certain route is active. It also includes helpers for retrieving the current controller and action names.


First, simply require the package through Composer.

composer require watson/active

Using Laravel 5.1? The latest version of the package that will work for you is 2.0.4.

Next, add the service provider in your config/app.php file.


If you'd like to use the Facade instead of the helper functions, add it to the aliases array.

'Active' => Watson\Active\Facades\Active::class

Using Active

Helper functions

Active ships with a couple of helper functions which make it easy to use without the facade or creating an instance of your own.


Using active()

You pass an array of routes or paths you want to see are the current page, and if any match this function will return the string active, for Bootstrap. Alternatively, you can pass a custom return string as the second argument.

active(['login', 'users/*', 'posts.*', '']); // Returns 'active' if the current route matches any path or route name.

active(['login', 'logout'], 'active-class'); // Returns 'active-class' if the current route is 'login' or 'logout'.

active(['login', 'logout'], 'active-class', 'fallback-class'); // Returns 'fallback-class' if the current route is not 'login' or 'logout'.

In the first example, the function will return the string active if the current path is login, starts with users/ or if the name of the current route is posts.create.

Do note that a number of argument types are provided: you can use a path string, you can use a path string with a wildcard (using the *) and you can also use named routes.

You can use this function with your links to give them an active state.

<a href="{{ route('posts.index') }}" class="{{ active('posts.index') }}">All posts</a>

You can also provide certain paths or routes to be exluded when being considered.

active(['pages/*', 'not:pages/contact'])

active(['pages.*', ''])

Using is_active()

This works much the same as active(), you can pass the paths and routes to it but instead it will return a boolean if the current page matches.

@if (is_active('posts/*'))
    You're looking at a blog post!

Additional helpers

Two additional functions are provided to get the current controller and action, if your routing is being handled by a controller for a request. These functions will return the lowercase controller/action name, without the method of the request. Here is an example for a request that is routed to `FooController@getBar':

$controller = controller_name(); // foo

$action = action_name(); // bar
You can’t perform that action at this time.