Skip to content

Adds support for Attribute-based Routing (MVP) #56424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

AmirHkrg
Copy link

This pull request introduces the foundational support for attribute-based routing in Laravel. It provides a clean, modern, and developer-friendly way to declare routes directly on controller methods.

Motivation

As PHP continues to evolve, attributes have become a powerful tool for declarative programming. Many modern frameworks have adopted them for routing, which helps co-locate route definitions with their corresponding logic, improving clarity and developer ergonomics. This PR aims to bring this same modern convenience to Laravel, embracing modern PHP capabilities more deeply.

Crucially, this system is designed to work in conjunction with the traditional file-based routing system, not as a replacement. Developers can use both methods side-by-side in the same application, giving them more flexibility in how they organize their projects.

Proposed Solution

The feature is opt-in and enabled via a new fluent method, withAttributeRouting(), in bootstrap/app.php.

  1. Enabling the Feature
    Developers can easily specify which directories should be scanned for web and api route groups. If called with no arguments, it uses a sensible default.
// in bootstrap/app.php

return Application::configure()
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
    )
    // Example 1: Enable with a sensible default
    // (Scans app/Http/Controllers for the 'web' group)
    ->withAttributeRouting()

    // Example 2: Or, provide explicit configuration
    // ->withAttributeRouting(
    //     web: app_path('Http/Web'),
    //     api: app_path('Http/Api')
    // )
    ->...
  1. Defining Routes
    Controllers are marked for discovery by implementing the AttributeRouteController interface. Routes and groups are then defined with simple attributes:
<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Attributes\{Get, Group, Post};
use Illuminate\Routing\Attributes\AttributeRouteController;

#[Group(prefix: 'users', name: 'users.', middleware: 'auth')]
class UserController extends Controller implements AttributeRouteController
{
    #[Get('/', name: 'index')]
    public function index()
    {
        // Route: GET /users
        // Name: users.index
    }

    #[Post('/', name: 'store')]
    public function store()
    {
        // Route: POST /users
        // Name: users.store
    }
}

Performance Considerations

The performance impact of this feature has been carefully considered. In local development, a negligible overhead of a few milliseconds is introduced by the file scanning process. However, in production, once routes are cached using php artisan route:cache, there is zero performance impact. The only change is a simpler and more enjoyable development workflow.

This is an MVP

This initial pull request implements the Minimum Viable Product (MVP) for this feature. The goal is to first agree on the fundamental architecture and API.

I have already locally developed and tested more advanced features that can be added in subsequent PRs upon approval of this core concept. These future possibilities include:

  • #[Resource] and #[ApiResource] attributes for full RESTful controller support.
  • Advanced #[Group] features like only and except for targeting specific methods.
  • scopeBindings support for both resources and groups.
  • An implicit discovery mode to reduce boilerplate.
  • Enhancements to Artisan commands (route:list and make:controller).

Next Steps & Collaboration

I am fully committed to seeing this feature through to completion. If this initial proposal is accepted by the team and the community, I am ready to collaborate on any necessary changes and submit the subsequent PRs to build out the full feature set.

@AmirHkrg AmirHkrg marked this pull request as draft July 26, 2025 02:06
@AmirHkrg AmirHkrg marked this pull request as ready for review July 26, 2025 02:13
@AmirHkrg AmirHkrg marked this pull request as draft July 26, 2025 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant