Download the PHP package tobento/service-routing without Composer
On this page you can find all versions of the php package tobento/service-routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package service-routing
Routing Service
The Routing Service provides a flexible way to build routes for any PHP application.
Table of Contents
- Getting started
- Requirements
- Highlights
- Documentation
- Router
- Basic Routing
- Routing Methods
- Uri Definitions
- Handler Definitions
- Parameters
- Url Generation
- More Routes Methods
- Group Routing
- Resource Routing
- Domain Routing
- Domain Routes
- Domain Url Generation
- Managing Domains
- Signed Routing
- Signed Routes
- Signed Url Generation
- Localization and Translation Routing
- Localize Routes
- Translatable Routes
- Matched Route Event
- Constrainer
- Dispatching Strategies
- Simple
- With PSR-7 Response
- With PSR-15 Middleware
- Credits
Getting started
Add the latest version of the routing service running this command.
Requirements
- PHP 8.4 or greater
Highlights
- Basic routing (GET, POST, PUT, PATCH, UPDATE, DELETE)
- Domain routing
- Group routing
- Resource routing
- Named routes
- Matched route handling
- Url and signed url generation
- PSR-15 middleware support
- Localization
- Autowiring
- Framework-agnostic, will work with any project
- Decoupled design
- Easily extendable or customizable
Documentation
Router
Basic Routing
Routing methods
Uri definitions
Handler definitions
The default RouteHandler supports autowiring and the following handler definitions.
Parameters
Name a route:
The main purpose for named routes is the generation of URLs. But they might be useful for any other cases too.
:warning: Named routes should be unique, otherwise the route got overwritten.
Adding middleware: see also With PSR-15 Middleware
Where constraint parameter: see also Constrainer
Query constraint parameter:
Domain: see also Domain Routing
Signed: see also Signed Routing
Matches:
BaseUrl:
Adding custom parameters:
Url Generation
The router provides a url() method for generating URLs from named routes:
Throwing vs. Non-Throwing Behavior
By default, the router throws a UrlException if the route name does not exist:
Safe Mode (throw: false)
You may disable exceptions by passing throw: false:
In this case, the router returns an instance of NullUrl, which also implements UrlInterface.
A NullUrl:
- can be safely cast to a string (resulting in an empty string)
- provides the attempted route name via
name() - provides the attempted parameters via
parameters() - supports all
UrlInterfacemethods with safe defaults
This is useful when generating links in views, notifications, or emails where the route may not exist in all application contexts.
More Routes methods
Custom Routes:
Get All Routes:
Get Named Route:
Get Matched Route:
Group Routing
You might use groups to share parameters across routes:
If the group uri definition has parameters, they are available on the routes:
Resource Routing
You may use resource routing for convenience:
This will produce the following routes:
| Method | Uri | Action / Controller method | Route name |
|---|---|---|---|
| GET | products | index | products.index |
| GET | products/create | create | products.create |
| POST | products | store | products.store |
| GET | products/{id} | show | products.show |
| GET | products/{id}/edit | edit | products.edit |
| PUT/PATCH | products/{id} | update | products.update |
| DELETE | products/{id} | delete | products.delete |
You might route only specific actions:
You might change the default where constraint:
Adding new or overwriting existing actions:
Middleware:
Adding additional route parameters for an action:
Adding additional route parameters for all actions:
With localization and translation:
Domain Routing
Domain Routes
multiple domains
You may add a route for multiple domains:
domain specific parameters
You may set domain specific parameters for each domain:
Domain Url Generation
Managing Domains
You may specify the domains in order to managing them at one place.
Signed Routing
Signed Routes
Add a signed route:
Add a signed route with validating on handler for custom response:
Signed Url Generation
Localization and Translation Routing
Localize Routes
Support only specific locales:
Omit locale in request uri:
Define current locale:
Rename locale uri definition:
Translatable Routes
Without locale uri definition
Support only specific locales:
Define locale fallbacks:
With locale uri definition:
Default parameters are always prioritized:
Matched Route Event
The default MatchedRouteHandler supports autowiring.
Constrainer
Add rule constraint to route:
Available Rules:
| Rule | Regex | Description |
|---|---|---|
| :alpha | [a-zA-Z]+ | |
| :alpha:2 | [a-zA-Z]{2} | n{x} Matches any string that contains a sequence of X n's |
| :alpha:2:5 | [a-zA-Z]{2,5} | n{x,y} Matches any string that contains a sequence of X to Y n's |
| :alpha:2: | [a-zA-Z]{2,} | n{x,} Matches any string that contains a sequence of at least X n's |
| :num | [0-9]+ | |
| :num:2 | [0-9]{2} | n{x} Matches any string that contains a sequence of X n's |
| :num:2:5 | [0-9]{2,5} | n{x,y} Matches any string that contains a sequence of X to Y n's |
| :num:2: | [0-9]{2,} | n{x,} Matches any string that contains a sequence of at least X n's |
| :alphaNum | [a-zA-Z0-9]+ | |
| :alphaNum:2 | [a-zA-Z0-9]{2} | n{x} Matches any string that contains a sequence of X n's |
| :alphaNum:2:5 | [a-zA-Z0-9]{2,5} | n{x,y} Matches any string that contains a sequence of X to Y n's |
| :alphaNum:2: | [a-zA-Z0-9]{2,} | n{x,} Matches any string that contains a sequence of at least X n's |
| :id:1:5 | :id:minNumber:maxLength | |
| :id | Uses the default parameters from the rule :id:1:21 | |
| :in:foo:bar:baz | If the value is is one of foo, bar, baz |
Custom Rules:
Dispatching Strategies
There are different ways of handling the matched route, depending on your needs.
Simple
No middleware support though.
With PSR-7 Response
No middleware support though.
With PSR-15 Middleware
You will need to define your MiddlewareDispatcher implementation on the container. You might customize this behaviour by your own RouteHandler though.
Credits
- Tobias Strub
- All Contributors
All versions of service-routing with dependencies
psr/http-message Version ^2.0
psr/http-server-handler Version ^1.0
psr/http-server-middleware Version ^1.0
tobento/service-collection Version ^2.0
tobento/service-support Version ^2.0
tobento/service-uri Version ^2.0
tobento/service-dater Version ^2.0
tobento/service-autowire Version ^2.0
tobento/service-middleware Version ^2.0