Skip to content
This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
/ active-state Public archive

Laravel link active state helper based on URL, route or query string

License

Notifications You must be signed in to change notification settings

Arcesilas/active-state

Repository files navigation

Active State

Packagist license Travis Scrutinizer

This package helps you check if a given link matches the current URL, route or query string. This is especially useful in sidebars.

History
This package was originally a fork from pyaesone17/active-state. To fix issue #8, I rewrote entirely the package.

Basically you would do like this:

<li class="sidebar {{ Request::is('post') ? 'active' : 'no' }} ">Post</li>
<li class="sidebar {{ Request::is('page') ? 'active' : 'no' }} ">Page</li>

With this package, you can make it shorter:

<li class="sidebar {{ active_path_is('post') }} ">Post</li>
<li class="sidebar {{ active_path_is('page') }} ">Page</li>

Or, if you prefer to check the route:

<li class="sidebar {{ active_route_in('users.list') }}">Users list</li>
<li class="sidebar {{ active_route_in('groups.list') }}">Groups list</li>

Keywords: route, url, query, menu, link, request, laravel, active

TL;DR

Maybe you just need the cheatsheet?

Table of contents

  1. Quickstart
  2. Installation
  3. Upgrade from v2.x to v3.x
  4. Usage
  5. Blade directives
  6. Additional features
  7. Cheatsheet

Quickstart

Install with Composer

composer require arcesilas/active-state:^4.0

Version 4 is currently in alpha version. If your configuration requires higher than alpha, make sure to specify the full version:

composer require arcesilas/active-state:^4.0.0-alpha

If you want the latest release:

composer require arcesilas/active-state:@dev-develop-v4

Use it in your views

Check current path is foo/bar:

<li class="menu-item {{ Active::ifPathIs('foo/bar') }}">Foo: Bar</li>

will render:

<li class="menu-item active">Foo: Bar</li>

if the current path is actually foo/bar.

Check route name is posts:

<a class="nav-link {{ Active::ifRouteIn('posts') }}" href="{{ route('posts') }}">

Check route name is posts and slug parameter is a given value:

<a class="nav-link {{Active::ifRouteIs('posts.category', ['slug' => $category->slug])}}" href="{{ route('posts.category', $category->slug) }}">

Check query string contains argument foo with value bar:

<a class="nav-link {{Active::ifUrlContains(['slug' => $category->slug])}}" href="{{ route('videos.category', $category->slug) }}">