Download the PHP package blackbonjour/slim-route-registry without Composer
On this page you can find all versions of the php package blackbonjour/slim-route-registry. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download blackbonjour/slim-route-registry
More information about blackbonjour/slim-route-registry
Files in blackbonjour/slim-route-registry
Package slim-route-registry
Short Description Attribute based route registry for Slim 4.
License MIT
Informations about the package slim-route-registry
Slim Route Registry
A PHP library that provides attribute-based route registration for Slim Framework applications. This library allows you to define routes using PHP 8 attributes on your handler classes and methods, making your code more declarative and organized.
Requirements
- PHP 8.2 or higher
- Slim Framework 4.14 or higher
- composer/class-map-generator 1.6 or higher
Installation
You can install the library via Composer:
Basic Usage
1. Create a Route Handler
Create a class that will handle your route and add the Route
attribute to it:
2. Register Routes with the RouteRegistry
In your application bootstrap file:
Advanced Usage
Named Routes
You can assign names to your routes for easier URL generation:
Multiple HTTP Methods
You can specify multiple HTTP methods for a single route:
Method-Level Routes
You can also define routes on public methods:
Route Arguments
You can pass additional arguments to your routes:
These arguments can be used to configure the route or provide additional data to the handler.
Middleware
You can attach middleware to your routes:
Redirects
You can define redirects using the Redirect
attribute:
You can also specify the HTTP status code for the redirect (defaults to 302):
When used with a Route
attribute, you can omit the destination path to redirect to the route's path:
API Reference
RouteRegistry
The main class responsible for registering routes.
$paths
: Array of directory paths to scan for route handler classes-
$classProvider
: Instance ofClassProviderInterface
(defaults toComposerClassProvider
) $app
: Slim Framework App instance
ClassProviderInterface
An interface for classes that provide a list of class names from a given path.
$path
: Directory path to scan for classes- Returns: Array of fully qualified class names
ComposerClassProvider
The default implementation of ClassProviderInterface
that uses Composer's ClassMapGenerator to find classes.
-
$classMapGenerator
: Optional custom instance of Composer's ClassMapGenerator (defaults to a new instance) $path
: Directory path to scan for classes- Returns: Array of fully qualified class names found in the path
- Throws:
DirectoryNotFoundExceptionClass
if the path is not a valid directory - Throws:
ClassMapGeneratorExceptionClass
if the ClassMapGenerator encounters an error
Route Attribute
$methods
: HTTP method(s) as string or array of strings (e.g., 'GET', ['GET', 'POST'])$path
: URL path pattern (e.g., '/users/{id}')$name
: Optional route name for URL generation$arguments
: Array of key-value pairs to be passed to the route$middlewares
: Array of middleware classes, callables, or string class names$redirects
: Array of Redirect objects
Redirect Attribute
$from
: Source path to redirect from$to
: Destination path (can be null if used with a Route attribute)$status
: HTTP status code for the redirect (defaults to 302)
License
This library is licensed under the MIT License. See the LICENSE file for details.