Installation:
composer require watson/active
Add to config/app.php:
'providers' => [
Watson\Active\ActiveServiceProvider::class,
],
(Optional) Add facade alias if preferred:
'aliases' => [
'Active' => Watson\Active\Facades\Active::class,
],
First Use Case: Check if the current route matches a specific route or URI:
// In a Blade template
<li class="{{ active('dashboard') ? 'active' : '' }}">Dashboard</li>
// In a controller
if (active('dashboard')) {
// Perform action
}
Key Methods to Explore:
active($route): Check if current route matches $route.activeRoute(): Get the current route name.activeController(): Get the current controller name.activeAction(): Get the current action/method name.Use active() in Blade templates to highlight the current menu item:
<nav>
<a href="{{ route('dashboard') }}" class="{{ active('dashboard') ? 'active' : '' }}">Dashboard</a>
<a href="{{ route('profile') }}" class="{{ active('profile') ? 'active' : '' }}">Profile</a>
</nav>
Restrict actions or modify behavior based on the current route:
public function index()
{
if (active('admin.dashboard')) {
return view('admin.dashboard');
}
return redirect()->route('home');
}
Dynamically apply middleware or logic in middleware:
public function handle($request, Closure $next)
{
if (active('admin.*')) {
// Custom logic for admin routes
}
return $next($request);
}
Use helper methods to inspect the current route context:
$routeName = activeRoute(); // e.g., 'admin.users.index'
$controller = activeController(); // e.g., 'Admin\UsersController'
$action = activeAction(); // e.g., 'index'
Create a custom Blade directive for reusable active state logic:
// In a service provider
Blade::directive('activeClass', function ($expression) {
return "<?php echo \\Watson\\Active\\Active::check($expression, 'active'); ?>";
});
// In Blade
<li class="{{ activeClass('dashboard') }}">Dashboard</li>
Combine with Form::open() or Link::route() for seamless integration:
{!! Form::open(['route' => 'dashboard']) !!}
<button type="submit" class="{{ active('dashboard') ? 'btn-primary' : 'btn-default' }}">
Submit
</button>
Use active() with Tailwind’s arbitrary variants:
<a href="{{ route('dashboard') }}" class="[&.active]:bg-blue-500">Dashboard</a>
(Note: Requires custom JS or a Blade directive to add the active class dynamically.)
Pass active state to Vue/React components:
return Inertia::render('Dashboard', [
'isActive' => active('dashboard'),
]);
Use active() to conditionally modify API responses:
return response()->json([
'data' => $data,
'meta' => [
'isAdminRoute' => active('admin.*'),
],
]);
php artisan route:cache), the active() checks may not reflect real-time changes.php artisan route:clear
Route::currentRouteName() directly in critical sections.activeController() and activeAction() may return unexpected results for:
App\Http\Controllers\UserController@index vs. UserController@show).Route::get('/test', [UserController::class, 'index'])->name('test');
Route::get('/test2', fn() => view('test'))->name('test2');
active('dashboard') won’t match active('Dashboard').snake_case).Active::check()) and helper (active()) may behave differently in some edge cases (e.g., nested checks).active() in tight loops (e.g., rendering a large menu) can be slow.$isActive = active('dashboard'); // Call once
foreach ($menuItems as $item) {
echo $isActive ? 'active' : '';
}
Dump the current route context for debugging:
dd([
'route' => activeRoute(),
'controller' => activeController(),
'action' => activeAction(),
'uri' => request()->path(),
]);
Ensure routes are registered as expected:
php artisan route:list
Look for typos or missing names.
active() DirectlyUse the helper in tinker to test:
php artisan tinker
>>> active('dashboard')
active() returns false for non-matching routes, but ensure your logic accounts for:
user.{id}).Extend the package to support custom active classes:
// In a service provider
Active::extend(function ($app) {
$app->singleton('active.class', function () {
return 'is-current';
});
});
// Usage
<li class="{{ active('dashboard', 'is-current') }}">Dashboard</li>
Extend active() to support route group prefixes:
// Hypothetical extension
if (active('admin.*')) { ... } // Matches all admin routes
Combine with route model binding for resourceful checks:
public function show(User $user)
{
if (active('users.edit', ['user' => $user->id])) {
// Custom logic
}
}
Publish the package config to override defaults:
php artisan vendor:publish --provider="Watson\Active\ActiveServiceProvider"
(Note: The package may not include publishable config; check for extension hooks.)
Extend to match routes with parameters:
// Hypothetical usage
active('posts.show', ['post' => 1]); // Matches /posts/1
Create reusable partials for navigation:
@component('partials.nav-item', ['route' => 'dashboard', 'label' => 'Dashboard'])
@endcomponent
<!-- partials/nav-item.blade.php -->
<li class="{{ active($route) ? 'active' : '' }}">
<a href="{{ route($route) }}">{{ $label }}</a>
</li>
Generate a JS file to handle active states dynamically:
// resources/js/active.js
document.querySelectorAll('[data-active]').forEach(el => {
if (window.location.pathname.includes(el.dataset.active)) {
How can I help you explore Laravel packages today?