Laravel Tutorials

Route Definition Enhancements in Laravel with Enum Integration

Published
Route Definition Enhancements in Laravel with Enum Integration image

Laravel continues to embrace PHP's enum functionality, now extending support directly to route definitions. This update eliminates the need for explicit value access when using enums in routes, resulting in cleaner, more maintainable code while preserving type safety.

When working with route names or domains, you can now pass enum cases directly to Laravel's routing methods, streamlining your route definitions without sacrificing the benefits of enum type checking.

enum RouteSection: string
{
    case Admin = 'admin';
    case Client = 'client';
    case Public = 'public';
}

Route::prefix(RouteSection::Admin)
    ->group(function () {
        // Admin routes
    });

Let's explore a practical example of organizing routes in a multi-portal application:

<?php

namespace App\Routing;

use App\Http\Controllers\Portal;

enum PortalType: string
{
    case Student = 'student.university.edu';
    case Faculty = 'faculty.university.edu';
    case Admin = 'admin.university.edu';
}

enum PortalSection: string
{
    case Dashboard = 'dashboard';
    case Resources = 'resources';
    case Profile = 'profile';
}

// Route configuration
Route::domain(PortalType::Student)
    ->middleware(['auth', 'student'])
    ->group(function () {
        Route::get('/', [Portal\StudentController::class, 'index'])
            ->name(PortalSection::Dashboard);
            
        Route::get('/materials', [Portal\ResourceController::class, 'index'])
            ->name(PortalSection::Resources);
            
        Route::get('/profile', [Portal\ProfileController::class, 'show'])
            ->name(PortalSection::Profile);
});

Route::domain(PortalType::Faculty)
    ->middleware(['auth', 'faculty'])
    ->group(function () {
        // Faculty portal routes using the same enums
        Route::get('/', [Portal\FacultyController::class, 'index'])
            ->name(PortalSection::Dashboard);
    });

This direct enum support in route definitions improves code readability while maintaining the type safety and autocompletion benefits of PHP enums.

Harris Raftopoulos photo

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

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Session Replay for Laravel: Self-Hosted Browser Recordings image

Session Replay for Laravel: Self-Hosted Browser Recordings

Read article
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article