Download the PHP package rancoud/router without Composer
On this page you can find all versions of the php package rancoud/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package router
Router Package
Router PSR7 and PSR15.
Installation
Dependencies
Http package: https://github.com/rancoud/Http
How to use it?
General Case
Routes shortcuts
Route Parameters
Use the pattern {name}
for naming your parameters
Constraints
Use regex syntax for your constraints
You can setup a global constraint when you use the same regex multiple times
You can use on each route an optional parameters.
The parameters {page}
will be replace with the value 1
if it is not present
Middlewares
Router in Route in Router
Default 404
WARNING with 404 and Router as Middleware
If you use Router as middleware AND set default 404 then findRouteRequest
and findRoute
will return true
.
When calling dispatch
it will use the callback setted as default 404 by the Router middleware.
Router Methods
General Commands
Add route
- addRoute(route: \Rancoud\Router\Route): void
Add route shortcuts
- get(url: string, callback: string|\Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router): \Rancoud\Router\Route
- post(url: string, callback: string|\Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router): \Rancoud\Router\Route
- put(url: string, callback: string|\Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router): \Rancoud\Router\Route
- patch(url: string, callback: string|\Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router): \Rancoud\Router\Route
- delete(url: string, callback: string|\Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router): \Rancoud\Router\Route
- options(url: string, callback: string|\Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router): \Rancoud\Router\Route
- any(url: string, callback: string|\Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router): void
Add route for a CRUD system
- crud(prefixPath: string, callback: string|\Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router): void
It will create all this routes:
GET $prefixPath
GET / POST $prefixPath . '/new'
GET / POST / DELETE $prefixPath . '/{id:\d+}'
Setup Router and Routes with an array
- setupRouterAndRoutesWithConfigArray(config: array): void
In this example you can setup router's middlewares and routes with an array
Get Routes
- getRoutes(): \Rancoud\Router\Route[]
Find route
- findRoute(method: string, url: string, [host: string = null]): bool
- findRouteRequest(request: \Psr\Http\Message\ServerRequestInterface): bool
- getRouteParameters(): array
Run the found route
- dispatch(request: \Psr\Http\Message\ServerRequestInterface): \Psr\Http\Message\Response
- handle(request: \Psr\Http\Message\ServerRequestInterface): \Psr\Http\Message\Response
The difference between dispatch and handle is dispatch is used in first place.
Handle is from the PSR17 in Psr\Http\Message\ServerRequestInterface, it's useful for middleware.
Middlewares
- addGlobalMiddleware(middleware: \Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router|string): void
- setGlobalMiddlewares(middlewares: array): void
Global constraints
- setGlobalParametersConstraints(constraints: array): void
- setGlobalHostConstraints(constraints: array): void
Generate url for a named route
- generateUrl(routeName: string, [routeParameters: array = []]): ?string
Host constraints
- setGlobalHost(host: string): void
Default 404
- setDefault404(callback: mixed): void
Route Constructor
Settings
Mandatory
Parameter | Type | Description |
---|---|---|
methods | string | array | methods matching with the route |
url | string | url to match |
callback | string | Closure | \Psr\Http\Server\MiddlewareInterface | \Rancoud\Router\Router | callback when route is calling by router |
Route Methods
General Commands
Getters/Setters
- getMethods(): array
- getUrl(): string
- getName(): string
- setName(name: string): void
Constraints
- setParametersConstraints(constraints: array): void
- getParametersConstraints(): array
- compileRegex(globalConstraints: array): string
- setOptionalsParameters(optionalsParameters: array): void
- getOptionalsParameters(): array
Callback
- getCallback(): mixed
Middlewares
- addMiddleware(middleware: \Closure|\Psr\Http\Server\MiddlewareInterface|\Rancoud\Router\Router|string): array
- getMiddlewares(): array
Generate Url
- generateUrl([routeParameters: array = []]): string
Host
- getHost(): ?string
- setHost(host: string, [hostConstraints: array = []]): void
- setHostConstraints(constraints: array): void
- isSameHost(host: string, globalConstraints: array = []): bool
- getHostParameters(): array
How to Dev
composer ci
for php-cs-fixer and phpunit and coverage
composer lint
for php-cs-fixer
composer test
for phpunit and coverage