netmex / laravel-attributes
A Laravel package to define routes using attributes.
Package info
github.com/netmexmedia/Netmex-Laravel-Attributes-Route
pkg:composer/netmex/laravel-attributes
Requires
- php: ^8.1
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
This package is not auto-updated.
Last update: 2026-03-30 13:17:15 UTC
README
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
Installation
Install the package via Composer:
composer require netmex/laravel-attributes
The 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, ];
Quick Usage
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() {} }
Service Provider & Services
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.
Configuration
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.
Commands
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:clear
Contributing
Contributions 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.
License
MIT License