Use “active” class for current route in Laravel 4

active

Updates: If you are using Laravel 5, read the documentation of the latest version instead.

When working with Laravel 4 and Bootstrap framework, I usually need determine  which menu item is currently “selected” and add the active class into that item. After installing the dwightwatson/active package and founding that that package does not fit my requirement, I decided to develop a new one and use it in my current project. You can find its source code in Github

Installation

You will need Composer to use this package in your work. Supposed that you have Composer installed in your machine, edit your project composer.json  file by adding this line into the require array:

"hieu-le/active": "~1.0"

After that, run composer update to get this package.

If you use Laravel 4, some extra steps need executing. Append these line in your providers and aliases array respectively:

# Add to `providers` array
'HieuLe\Active\ActiveServiceProvider',

# Add to `aliases` array
'Active' => 'HieuLe\Active\Facades\Active',

 Usage

Check whether current route match a pattern

# Return string 'active' if current URI is /foo
Active::pattern('foo'); 

# Return string 'selected' if current URI is /foo/bar, /foo/baz/lorem, ...
Active::pattern('foo/*', 'selected'); 

# Return string 'selected' if current URI is /foo or /bar
Active::pattern(array('foo', 'bar'), 'selected');

Check whether current route match a named route

# Return string 'active' if current route name is 'foo'
Active::route('foo');

# Return string 'selected' if current route name is 'foo' or 'bar'
Active::route(array('foo', 'bar'), 'selected')

Check whether current route match with an action formated as ‘[email protected]

# Return string 'active' if current route lead to the method 'getFoo' of the class 'FooController'
Active::action([email protected]');

# Return string 'selected' if current route lead to the method 'getFoo' of the class 'FooController' or the method 'postBar' of the class 'BarController'
Active::action(array([email protected]', [email protected]'), 'selected');

Check whether current controller is an instance of ‘FooController’ class

# Return 'active' if current controller is an instance of the 'FooController' class
Active::controller('Foo');

# Return 'selected' if current controller is an instance of 'FooController' class and current method is not 'getFoo'
Active::controller('Foo', 'selected', array('getFoo'));

Check whether current controller is an instance of ‘FooController’ class or ‘BarController’ class

# Return 'active' class if current controller is an instance of 'FooController' or 'Namespace\BarController' class
Active::controllers(array('Foo', 'Namespace\Bar'));

# Return 'selected' instead
Active::controllers(array('Foo', 'Namespace\Bar'), 'selected');

If you just want to know current controller and current method name, use these method. Notice that the “Controller” suffix will be trimmed from controller name and those prefix: “get”, “post”, “put”, “delete”, “show” will be removed from method name

# Return 'Foo' if current controller class is 'FooController'
# Return 'Namespace\Foo' if current controller class is 'Namespace\FooController'
Active::getController();

# Return 'Foo' if current method is 'getFoo' or 'postFoo'
Active::getMethod();

New method in version 1.2

Now, with my last update, you can check whether the current route name matches with one of predefined patterns by the suporting of the new method Active::routePattern Here are some examples:

# Return active for all routes that have name like "*.create"
Active::routePattern("*.create");

# Return active for all routes that have name like "*.post.index" or "*.post.edit"
Active::routePattern(['*.post.index', '*.post.edit'], "selected");

Notice that from version 1.2, this package does not support PHP 5.3 because of Laravel dependencies.

For more detail about API, your can see the class source code file

 

 

 

10 comments

    • Trung-Hieu Le Post authoron

      Did you run composer update recently? I have released some patches since laravel 4.2.

      Reply
  1. J1mb0_o on

    Hey thanks for this article, there is just one mistake in “Check whether current route match with an action formated as [email protected]” section. You wrote “Action::action…” instead of “Active::action…”

    Thanks again 😉

    Reply
  2. Pingback: Update my 'active' package to support Laravel 4.2 - Hieu Le Trung's personal tech blog

  3. Jeet Dholakia on

    Hello,

    I am very new to Laravel and I am developing a site in Laravel 4.2. I want to set class as active on which ever page I am i.e. If I am on home then there should be active class to it. I am not getting how to properly add it.

    Below is my code:

    <a href="route('home')}}" rel="nofollow">Home</a>

    Thank you in advance.

    Reply
    • Hieu Le Post authoron

      You can use this package to add the active class to the anchor element:

      <a href="{{route(home)}}" class="{{Active::route('home')}}"> Home </a>

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *