Download the PHP package bermudaphp/router without Composer
On this page you can find all versions of the php package bermudaphp/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bermudaphp/router
More information about bermudaphp/router
Files in bermudaphp/router
Package router
Short Description Flexible and performant routing library for PHP 8.4+ with route caching support
License MIT
Informations about the package router
Bermuda Router
๐ท๐บ ะ ัััะบะฐั ะฒะตััะธั
Flexible and performant routing library for PHP 8.4+ with route caching support.
Table of Contents
- Installation
- Quick Start
- Creating Routes
- HTTP Verb Helper Methods
- Using RouteBuilder
- Route Parameters
- Basic Parameters
- Predefined Patterns
- Custom Patterns
- Default Values
- Route Groups
- Group Configuration
- URL Generation
- PSR-15 Middleware Integration
- Basic Setup
- Using RouteNotFoundHandler
- Configuring 404 Handler
- Integration in Middleware Pipeline
- Accessing Route Data in Controllers
- Route Locators
- Locator Setup
- Routes File
- PHP Attribute-based Route Location
- Installation
- Route Attribute
- AttributeRouteLocator Setup
- ClassFinder Integration
- Accessing Route Handler
- Route Caching
- Creating Cache
- Using Cache
- Cache with Context for Closures
- Caching Limitations
- Error Handling
- Exception Types
Installation
Requirements: PHP 8.4+
Quick Start
Creating Routes
HTTP Verb Helper Methods
Method | HTTP Methods | Description | Usage Example |
---|---|---|---|
get() |
GET | Retrieve data | RouteRecord::get('users.index', '/users', 'UsersController') |
post() |
POST | Create new resources | RouteRecord::post('users.store', '/users', 'UsersController::store') |
put() |
PUT | Full resource update | RouteRecord::put('users.update', '/users/[id]', 'UsersController::update') |
patch() |
PATCH | Partial resource update | RouteRecord::patch('users.patch', '/users/[id]', 'UsersController::patch') |
delete() |
DELETE | Delete resource | RouteRecord::delete('users.destroy', '/users/[id]', 'UsersController::destroy') |
head() |
HEAD | Retrieve headers | RouteRecord::head('users.check', '/users/[id]', 'UsersController::head') |
options() |
OPTIONS | Get available methods | RouteRecord::options('users.options', '/users', 'UsersController::options') |
any() |
Custom | Multiple HTTP methods | RouteRecord::any('users.resource', '/users/[id]', 'UsersController', ['GET', 'PUT', 'DELETE']) |
Using RouteBuilder
Route Parameters
Basic Parameters
Predefined Patterns
Name | Pattern | Description | Examples |
---|---|---|---|
id |
\d+ |
Numeric ID | 1, 123, 999 |
slug |
[a-z0-9-]+ |
URL-compatible string | hello-world, my-post |
uuid |
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} |
UUID v4 format | 550e8400-e29b-41d4-a716-446655440000 |
any |
.+ |
Any characters including slashes | any/path/here |
alpha |
[a-zA-Z]+ |
Letters only | Hello, ABC |
alnum |
[a-zA-Z0-9]+ |
Letters and digits | Hello123, ABC789 |
year |
[12]\d{3} |
4-digit year (1900-2999) | 2024, 1995 |
month |
0[1-9]\|1[0-2] |
Month (01-12) | 01, 12 |
day |
0[1-9]\|[12]\d\|3[01] |
Day of month (01-31) | 01, 15, 31 |
locale |
[a-z]{2}(_[A-Z]{2})? |
Locale code | en, en_US, fr_FR |
version |
v?\d+(\.\d+)* |
Version string | 1.0, v2.1.3 |
date |
\d{4}-\d{2}-\d{2} |
ISO date (YYYY-MM-DD) | 2024-12-25 |
Custom Patterns
Inline Patterns
Inline patterns allow defining regex patterns directly in route definition. Syntax: [parameter_name:regular_expression]
Setting Patterns via Methods
Pattern Priority
Patterns are applied in the following priority order (highest to lowest):
- Inline patterns in route:
[id:\d+]
- Method patterns:
->withToken('id', '\d+')
- Group patterns:
$group->setTokens(['id' => '\d+'])
- Predefined patterns: from table above
- Default pattern:
[^\/]+
(any characters except slash)
Default Values
Route Groups
Groups allow organizing related routes with common settings:
Group Configuration
URL Generation
PSR-15 Middleware Integration
Basic Setup
Using RouteNotFoundHandler
RouteNotFoundHandler handles requests for non-existent routes and can work in two modes:
Integration in Middleware Pipeline
Accessing Route Data in Controllers
Route Locators
For loading routes from configuration files:
Locator Setup
Routes File
PHP Attribute-based Route Location
The library supports automatic route discovery through PHP attributes on controller methods. This allows defining routes declaratively, right next to handlers.
Installation
Attribute support requires an additional package:
Route Attribute
The #[Route]
attribute allows defining routes directly on controller methods:
Route Attribute Parameters
Parameter | Type | Description | Example |
---|---|---|---|
name |
string |
Unique route name | 'users.show' |
path |
string |
URL pattern with parameters | '/users/[id]' |
methods |
string\|array |
HTTP methods (string or array) | 'GET' , 'PUT\|PATCH' , ['GET', 'POST'] |
middleware |
array |
Middleware array for route | ['auth', 'validation'] |
group |
string |
Route group name | 'api' |
priority |
int |
Route priority (higher = earlier) | 10 |
defaults |
array |
Default parameter values | ['format' => 'json'] |
AttributeRouteLocator Setup
AttributeRouteLocator works as a decorator for existing route locators:
ClassFinder Integration
For automatic controller discovery with attributes, ClassFinder is used (already included in dependencies):
๐ Detailed documentation: bermudaphp/finder | Russian Guide
Full Application Integration
Groups via Attributes
โ ๏ธ Important: Route groups must be predefined in code (e.g., in base locator's routes file), otherwise RouterException
will be thrown.
Route Priorities
Priorities determine the order of route checking when matching requests. Routes with higher priority are checked first.
Priority Rules:
- Default priority =
0
- Higher number = higher priority
- Routes are sorted by descending priority (100, 50, 10, 0, -10)
- Order not guaranteed for same priority
When to use priorities:
- Special routes should be checked before general ones
- Specific patterns โ before wide catch-all routes
- API versioning with fallback to older versions
Priority example for API versioning:
Regular routes vs attribute routes:
- Attribute routes: priority determined by
priority
parameter - Regular routes: priority determined by addition order (first added = highest priority)
Accessing Route Handler
RouteRecord provides convenient access to various route components:
Route Caching
For improved performance in production:
Creating Cache
Using Cache
Cache with Context for Closures
When routes use closures with external variables (via use
), these variables must be available when loading cached routes. Context allows passing necessary objects and data to the cached file scope.
Caching Limitations
The caching system has the following limitations:
โ What cannot be cached
โ What can be cached
๐ก Recommendations
- Use string handlers in production for maximum cache compatibility
- Pass contextual data if using
use
in closure handlers
Most preferred handler type - class name or class and method combination
Error Handling
Exception Types
All versions of router with dependencies
bermudaphp/psr15factory Version ^3.0
bermudaphp/var-export Version ^2.0.1
bermudaphp/number Version ^2.0
bermudaphp/config Version ^2.0
fig/http-message-util Version ^1.1
psr/http-message Version ^2.0
psr/http-server-handler Version ^1.0
psr/http-server-middleware Version ^1.0