Lightweight Laravel package to define and register HTTP routes using PHP Attributes on controllers and methods.
Key features
- Define routes declaratively with a
#[Route(...)]PHP Attribute on controllers and methods. - Convert attributes into a compact
RouteMetadatavalue object via a parser. - Cache discovered metadata with optional automatic invalidation based on controller file mtimes.
- A
Registrarservice that wires metadata into the Laravel router (route registration and model binding helpers).
Table of Contents
- Installation
- Quick Usage
- Class-level group attributes
- Service Provider
- Configuration
- Commands
- Contributing
- License & Maintainer
Install the package via Composer:
composer require netmex/laravel-attributesThe package supports Laravel's package auto-discovery. If you don't use auto-discovery, register the service provider in your application:
// config/app.php
'providers' => [
// ...
Netmex\Attributes\Route\AttributesRouteServiceProvider::class,
];Annotate controller classes and methods using the package attribute Netmex\Laravel\Attributes\Route.
Method-level example:
use Netmex\Laravel\Attributes\Route;
final class PostController
{
#[Route(path: '/posts', name: 'posts.index', methods: ['GET'])]
public function index()
{
// controller action
}
#[Route(path: '/posts/{id}', name: 'posts.show', methods: ['GET'], requirements: ['id' => '\\d+'])]
public function show(int $id)
{
// controller action
}
}Class-level (group) example — apply a prefix and default middleware or naming:
use Netmex\Laravel\Attributes\Route;
#[Route(path: '/admin', name: 'admin.', middleware: ['auth'])]
final class Admin\DashboardController
{
#[Route(path: '/dashboard', name: 'dashboard')]
public function index() {}
}The package registers the following services in the container via AttributesRouteServiceProvider:
Discovery\ControllerDiscovery— discover controller classes from the configured path/namespace.Parser\AttributeParser— parse attributes (Reflection) intoRouteMetadatainstances.Cache\RouteMetadataCache— cache and return metadata arrays/objects.Registrar\RegistrarandRegistrar\RouteRegistrar— register routes intoIlluminate\Routing\Routerand apply model binding.
Publish or review config/attributes-route.php for the following options:
cache_key(string) — cache key prefix (default:attributes_route_metadata).cache_ttl(int|null) — TTL in seconds (null = forever).auto_invalidate(bool) — append a mtime-based hash to the cache key so metadata is automatically invalidated when controller files change.
The package includes a console command to clear the metadata cache. You can call it via artisan (command name is provided by the package service provider):
php artisan attributes-route:clearContributions welcome. Suggested improvements and tests:
- Integration tests using
Orchestra\Testbenchto assertRegistrar::register()registers routes into the router. - Tests for the console cache-clear command and cache invalidation behavior.
- Add documentation examples showing
php artisan route:listafter registration.
MIT License