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 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

Use PHP 8 attributes to register routes in a Laravel app

Frequently asked questions about Laravel Route Attributes
How do I migrate from traditional `Route::get()` to attribute-based routing in Laravel?
Start by replacing one route at a time, using attributes like `[Get('path')]` on controller methods. Run `php artisan route:list` to verify registration. For gradual adoption, keep legacy routes in `routes/web.php` and new ones in controllers. Use feature flags to toggle between approaches during transition.
Will attribute-based routes work with Laravel’s route caching (`route:cache`)?
Yes, the package fully supports Laravel’s route caching. After defining attribute routes, run `php artisan route:cache` to optimize performance. Test caching in staging to ensure no conflicts with traditional routes or middleware.
Does this package support middleware, authentication, or API resources like traditional Laravel routing?
Absolutely. Attribute routes support middleware (e.g., `#[Get('path', middleware: ['auth'])]`), authentication guards, and API resources out of the box. The syntax mirrors Laravel’s native routing conventions, so existing logic (e.g., `Route::apiResource()`) translates seamlessly.
Can I use attribute routing alongside traditional `Route::` definitions in the same Laravel app?
Yes, the package enables hybrid routing. Attribute routes and traditional routes coexist without conflicts. Attribute routes override traditional ones only if they share the same URI. This makes incremental adoption safe and flexible.
What Laravel versions and PHP versions does `spatie/laravel-route-attributes` support?
The package supports Laravel 9+ (PHP 8.0+) and is actively maintained. It leverages PHP 8+ attributes, so ensure your project meets these requirements. Check the [GitHub releases](https://github.com/spatie/laravel-route-attributes/releases) for the latest compatibility updates.
Will attribute-based routing impact performance in high-traffic Laravel applications?
Reflection overhead is minimal for most use cases. Benchmark with `php artisan route:list` and `route:cache` to measure impact. For high-throughput systems, test under load, but the package is optimized for production and widely used without issues.
How do I test attribute-routed endpoints in Laravel/PestPHP?
Use Laravel’s `Route::getRoutes()` to verify routes are registered. Test endpoints like traditional routes (e.g., `get('/path')->assertOk()`). Update existing tests to account for attribute-based definitions, ensuring coverage for middleware, authentication, and dynamic parameters.
Are there any limitations with dynamic routes (e.g., regex, optional parameters) using attributes?
Complex dynamic routes (e.g., regex patterns or optional parameters) may require manual `Route::` definitions. For simple cases, use attributes like `[Get('users/{id?}')]`. Test edge cases in staging to ensure behavior matches expectations.
How does IDE autocompletion (PHPStorm/VSCode) handle attribute-based routes?
IDE support for PHP 8 attributes is improving but may lag behind traditional `Route::` definitions. Use `@method` docblocks (e.g., `/** @method void myMethod() */`) to aid autocompletion. The package’s syntax is intuitive, reducing reliance on IDE-specific features.
What alternatives exist to attribute-based routing in Laravel, and why choose this package?
Alternatives include manual `Route::` definitions or packages like `laravel-attributes` (less maintained). This package is battle-tested (879 stars), integrates natively with Laravel’s DI and middleware, and offers a clean, declarative syntax. It’s ideal for teams already using PHP 8 attributes or seeking modern routing.
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