Download the PHP package davidecesarano/embryo-routing without Composer
On this page you can find all versions of the php package davidecesarano/embryo-routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download davidecesarano/embryo-routing
More information about davidecesarano/embryo-routing
Files in davidecesarano/embryo-routing
Package embryo-routing
Short Description A PHP Router PSR compatible.
License MIT
Homepage https://github.com/davidecesarano/embryo-routing
Informations about the package embryo-routing
Embryo Routing
A lightweight, fast and PSR compatible PHP Router.
Features
- PSR (7, 11, 15) compatible.
- Static, dynamic and optional route patterns.
- Supports GET, POST, PUT, PATCH, DELETE and OPTIONS request methods.
- Supports route middlewares.
- Supports grouping routes.
- Supports resolve via Container.
- Works in subfolders.
Requirements
- PHP >= 7.1
- URL Rewriting
- A PSR-7 http message implementation and PSR-17 http factory implementation (ex. Embryo-Http)
- A PSR-11 container implementation (ex. Embryo-Container)
- A PSR-15 http server request handlers implementation (ex. Embryo-Middleware)
Installation
Using Composer:
Example
Before defining the application routes, it is necessary to create the following objects:
- the
Container
- the
ServerRequestFactory
- the
ResponseFactory
- the
RequestHandler
- the
Emitter
Later, you can define the routes with Router
object:
Now, create PSR-15 middleware queue adding required routing middlewares:
MethodOverrideMiddleware
for overriding the HTTP Request Method.RoutingMiddleware
for match route and handler discovery.RequestHandlerMiddleware
for executing request handlers discovered by router.
Finally you can produce output of the Response with Emitter
object.
Usage
- Create routes
- Callbacks
- Placeholders
- Set name route
- Create route groups
- Add middleware to route
- Resolve via Container
- Set default namespace
- Working in subfolder
Create routes
You can define application routes using methods on the Router object. Every method accepts two arguments:
- The route pattern (with optional placeholders)
- The route callback (a closure, a
class@method
string or a['class', 'method']
array)
Note that you can write pattern with or without "/" to first character like this: blog/{id}
.
Methods
Embryo Routing supports GET, POST, PUT, PATCH, DELETE and OPTIONS request methods. Every request method corresponds to a method of the Router object: get()
, post()
, put()
, patch()
, delete()
and options()
.
You can use all()
and map()
methods for supporting all methods or specific route methods.
Overriding the request method
Use X-HTTP-Method-Override
to override the HTTP Request Method. Only works when the original Request Method is POST. Allowed values for X-HTTP-Method-Override are PUT
, DELETE
, or PATCH
. Embryo uses MethodOverrideMiddleware
for manage HTTP-Method-Override.
Callbacks
Each routing method accepts a callback routine as its final argument. This argument by default it accepts at least two arguments:
- Request. The first argument is a
Psr\Http\Message\ServerRequestInterface
object that represents the current HTTP request. - Response. The second argument is a
Psr\Http\Message\ResponseInterface
object that represents the current HTTP response. - Placeholder(s). Each route placeholder as a separate argument.
Writing content to the response
There are three ways you can write content to the HTTP response:
- You can simply
echo()
content from the route callback: this content will be appended to the current HTTP response object. - You can return a
Psr\Http\Message\ResponseInterface
object. - You can return a
json
content when returning an array.
Closure binding
If you use a Closure instance as the route callback, the closure’s state is bound to the Container
instance. This means you will have access to the DI container instance inside of the Closure via the $this
keyword:
Access to current route
If you get the route's object, you can it using the request attribute:
Placeholders
Route patterns may use named placeholders to dynamically match HTTP request URI segments.
Format
A route pattern placeholder starts with a {
, followed by the placeholder name, ending with a }
. Name and value placeholder may be each character from a-z, A-Z, 0-9, including the _ (underscore).
Optional
To make a placeholder optional, wrap it in square brackets:
You can use a multiple optional parameters:
For "simple slug" optional parameters, you can do this:
In this example, a URI of /blog/2018/my-post-title
would result in the $year
(2018) and $slug
(my-post-title) arguments.
Set regex route
By default the placeholders can accept any character allowed for composing an uri with the exception of the /
character. However, placeholders can also require the HTTP request URI to match a particular regular expression. For this, you can use where()
method:
Set name route
You can be assigned a name at the route with name()
method:
Create route groups
You can organize routes into logical groups with group()
method. If you want add a route prefix you can use prefix()
method:
In this example URI is, for example, /api/user/1.
Add middleware to route
You can also attach a PSR-15 middleware to any route or route group. The middleware()
method accepts a string, an array or an instance of Psr\Http\Server\MiddlewareInterface
.
Route middleware
You can use the middleware()
method to assign one or more middleware at the route:
Group middleware
In addition to the routes, you can assign one or more middleware to a group and to individual routes within the group:
Resolve via Container
In callback, in addition to closure, you can use the class@method
string or ['className', 'methodName']
array:
It translates into a class/method call like this:
In this example you will have access to the DI container instance inside of the class via the $this
keyword.
If you want have access to service in container use $this->get('myservice')
.
If you want have access to the request or response instance use $this->request
and $this->response
.
In addition, methods parameters it's may also called using type-hinting
, which means you can put the class you need in the method and the Service Container will resolve it automatically:
Set default namespace
You may to set default namespace for your controllers with setNamespace()
method:
Working in subfolder
Embryo Routing can works in a subdirectory by setting the path with setBasePath()
method:
All versions of embryo-routing with dependencies
davidecesarano/embryo-http Version dev-master
davidecesarano/embryo-container Version dev-master
davidecesarano/embryo-middleware Version dev-master
davidecesarano/embryo-emitter Version dev-master