Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Route Attributes Laravel Package

spatie/laravel-route-attributes

Register Laravel routes using PHP 8 attributes on controller methods (Get/Post/etc.). Automatically scans configured controller directories and registers routes without manual Route:: definitions. Includes config publishing and optional enabling/disabling of auto registration.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Developer Experience (DX) & Productivity:

    • Accelerate backend API development by reducing boilerplate route definitions in routes/web.php or routes/api.php.
    • Enable self-documenting routes tied directly to controller methods, improving code readability and maintainability.
    • Align with modern PHP 8+ attribute-based patterns, reducing cognitive load for teams adopting newer PHP features.
  • API/Backend Roadmap:

    • Build vs. Buy: Avoid reinventing route registration logic; leverage a battle-tested, community-supported solution.
    • Modularity: Simplify refactoring or splitting monolithic controllers into smaller, attribute-annotated services (e.g., for microservices or modular monoliths).
    • Consistency: Standardize route definitions across teams, reducing inconsistencies in URL patterns or middleware.
  • Use Cases:

    • Rapid Prototyping: Quickly spin up endpoints for MVPs or internal tools without manual route registration.
    • Legacy Modernization: Gradually migrate traditional Route::get() definitions to attributes in large codebases.
    • API-First Development: Pair with OpenAPI/Swagger tools (e.g., darkaonline/l5-swagger) to auto-generate API docs from attributes.
    • Middleware/Authorization: Combine with attributes like #[Middleware('auth:sanctum')] or custom attributes for declarative route policies.
  • Architectural Decisions:

    • Separation of Concerns: Decouple route definitions from routes/*.php files, centralizing logic in controllers.
    • Testing: Simplify route testing by leveraging PHPUnit’s attribute reflection (e.g., #[RouteTest] annotations for test suites).

When to Consider This Package

  • Adopt When:

    • Your team is using Laravel 8+ (PHP 8 attributes required).
    • You prioritize developer velocity over minimalist route files.
    • Routes are highly dynamic (e.g., tenant-specific, feature-flagged, or generated from business logic).
    • You’re adopting attribute-based patterns (e.g., for DDD, CQRS, or event-driven architectures).
    • Your API routes are stable but frequently updated (reduces merge conflicts in routes/*.php).
  • Look Elsewhere If:

    • You need complex route logic (e.g., regex patterns, locale prefixes) that attributes can’t easily express.
    • Your team prefers explicit route definitions for clarity or auditability (e.g., regulatory compliance).
    • You’re using Laravel < 8.0 (attributes not supported).
    • Routes require runtime modifications (e.g., dynamic route generation via middleware).
    • You’re constrained by legacy systems where modifying controllers is risky (e.g., monolithic apps with tight coupling).

How to Pitch It (Stakeholders)

For Executives/Product Leaders:

"This package lets our backend team define API routes directly in controller methods using PHP attributes—eliminating repetitive route boilerplate in routes/*.php files. It’s like moving route definitions closer to the business logic they serve, which speeds up development, reduces errors, and makes the codebase easier to navigate. For example, a new endpoint can be added in seconds without touching route files. It’s a low-risk, high-reward DX improvement that aligns with modern PHP practices and could reduce technical debt in our API layer."

Key Outcomes:

  • Faster iteration on APIs (critical for [Product Initiative X]).
  • Fewer merge conflicts in shared route files.
  • Easier onboarding for new devs (routes are "attached" to their handlers).

For Engineering Teams:

*"This replaces manual Route::get() calls with attributes like #[Get('path')], auto-registering routes at runtime. Benefits:

  • No more route file sprawl: All routes live in controllers where they’re used.
  • Seamless testing: Attributes make it trivial to mock or reflect routes in tests.
  • Future-proof: Works with Laravel’s evolving attribute ecosystem (e.g., middleware, validation).
  • Zero config: Just install, and it works—no complex setup.

Trade-offs:

  • Slightly less explicit route visibility (though IDEs/tooling can mitigate this).
  • Not ideal for highly dynamic routes (e.g., those generated at runtime).

Proposal: Pilot this in [Module Y] to compare DX with our current approach. If successful, we can roll it out to new APIs and gradually migrate existing ones."*


Visual Hook: "Imagine routes/api.php shrinking from 500 lines to 50, with routes defined next to their logic—like how React routes work with Route components."

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport