Laravel Tutorials

Smart Route Detection in Laravel

Published
Smart Route Detection in Laravel image

Laravel's named method provides a clean way to determine if a current request matches a specific route name. This powerful feature allows you to execute conditional logic based on the current route, perfect for analytics, navigation highlighting, or permission checks.

This approach becomes particularly valuable when building components that need to behave differently based on the current route, without writing repetitive conditional checks throughout your application.

if ($request->route()->named('dashboard')) {
    // We're on the dashboard
}

Here's a practical example implementing dynamic navigation states:

<?php

namespace App\View\Components;

use Illuminate\View\Component;
use Illuminate\Http\Request;

class NavigationMenu extends Component
{
    public function __construct(private Request $request)
    {
    }

    public function isActive(string $routeName): bool
    {
        return $this->request->route()->named($routeName);
    }

    public function isActiveSection(string $section): bool
    {
        return $this->request->route()->named("$section.*");
    }

    public function render()
    {
        return view('components.navigation-menu', [
            'sections' => [
                'dashboard' => [
                    'label' => 'Dashboard',
                    'route' => 'dashboard',
                    'active' => $this->isActive('dashboard')
                ],
                'posts' => [
                    'label' => 'Blog Posts',
                    'route' => 'posts.index',
                    'active' => $this->isActiveSection('posts')
                ],
                'settings' => [
                    'label' => 'Settings',
                    'route' => 'settings.index',
                    'active' => $this->isActiveSection('settings')
                ]
            ]
        ]);
    }
}

When used in your application, the navigation component automatically detects the current route and updates accordingly:

<!-- navigation-menu.blade.php -->
<nav>
    @foreach($sections as $key => $section)
        <a href="{{ route($section['route']) }}"
           @class(['nav-link', 'active' => $section['active']])>
            {{ $section['label'] }}
        </a>
    @endforeach
</nav>

The named method simplifies route-based logic, making your code more maintainable and reducing the complexity of route-dependent features.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article
PIE 1.5 Changes PHP Extension Installs in CI image

PIE 1.5 Changes PHP Extension Installs in CI

Read article
Delete Expired Sessions in Laravel With a Scheduled Command image

Delete Expired Sessions in Laravel With a Scheduled Command

Read article
Pollora: Run WordPress Inside a Laravel Application image

Pollora: Run WordPress Inside a Laravel Application

Read article
Scheduled Session Garbage Collection in Laravel 13.39 image

Scheduled Session Garbage Collection in Laravel 13.39

Read article