Download the PHP package phel-lang/router without Composer
On this page you can find all versions of the php package phel-lang/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phel-lang/router
More information about phel-lang/router
Files in phel-lang/router
Package router
Short Description A Phel router based on symfony routing component
License MIT
Homepage https://phel-lang.org/
Informations about the package router
Phel router
A data driver router for Phel.
- Based on Symfony Routing
- Inspired by reitit
- Fast
Installation
Route syntax
Routes are defined as vectors. The first element is the path of the route. This element is followed an optional map for route data and an optional vector of child routes. The paths of a route can have path parameters.
Examples
Simple route:
Two routes:
Routes with data:
Routes with path parameters:
Routes with path parameter validation:
Routes with catch all parameters:
Nested routes:
Same routes flattened:
Router
Given a vector of routes a router can be create. Phel router offers two option to create a router. The first is a dynamic router that is evaluated with every new request:
The second router is a compiled router. This router is evaluated during compile time (macro) and is therefore very fast. The drawback is that routes can not be created dynamically during the execution of a request.
Path based routing
To match a route given a path the phel\router/match-by-path
function can be used. It takes a router and a path as arguments and returns a map or nil
.
Name based routing
All routes that have :name
route data can be matched by name using the phel\router/match-by-name
function. It takes a router and the name of route and returns a map or nil
.
Generate path
It is also possible to generate a path for a give route and it's path parameters. The function phel\router/generate
takes a router, the name of the route and a map of router parameters. It returns either the generate path as string or throws an exception if the route can not be found ore path parameters are missing.
Handler
Each route can have a handler functions that can are execute when a route matches the current path. A handler function is a function that takes as argument as phel\http/request
and returns a phel\http/response
.
A handler can be placed either at the top level of the route data using the :handler
keyword or under a specific method (:get
, :head
, :patch
, :delete
, :options
, :post
, :put
or :trace
). The top level handler is used if a request method based handler is not found.
To process a request the router must be wrapped in the phel\router/handler
method. This method returns a function that accepts a phel\http/request
and returns a phel\http/response
.
Middleware
Each router can have multiple middleware functions. A middleware function is a function that takes a handler function and a phel\http/request
and returns a phel\http/response
.
A middleware can be placed either at the top level of the route data using the :middleware
keyword or under a specific method (:get
, :head
, :patch
, :delete
, :options
, :post
, :put
or :trace
).
All versions of router with dependencies
phel-lang/phel-lang Version >=0.17
gacela-project/gacela Version >=1.9
symfony/routing Version >=7.3