PHPackages                             chimera/routing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. chimera/routing

ActiveLibrary

chimera/routing
===============

A collection of reusable PSR-15 components that connects Chimera to any framework

0.4.0(5y ago)4180.4k↓38.2%[11 PRs](https://github.com/chimeraphp/routing/pulls)2MITPHPPHP ^7.4 || ^8.0

Since May 5Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/chimeraphp/routing)[ Packagist](https://packagist.org/packages/chimera/routing)[ GitHub Sponsors](https://github.com/lcobucci)[ Patreon](https://www.patreon.com/lcobucci)[ RSS](/packages/chimera-routing/feed)WikiDiscussions 1.0.x Synced 1mo ago

READMEChangelog (8)Dependencies (17)Versions (25)Used By (2)

Chimera - routing
=================

[](#chimera---routing)

[![Total Downloads](https://camo.githubusercontent.com/4116f78d9a1bcf6cb0a933daae26677bfab2d8104da521b8a73f1e598464e8e3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6368696d6572612f726f7574696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/chimera/routing)[![Latest Stable Version](https://camo.githubusercontent.com/2f786b2e59cad206abf1a799b9500d31a189e85919f57a23f5b308de50e8a1b1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368696d6572612f726f7574696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/chimera/routing)[![Unstable Version](https://camo.githubusercontent.com/072a75c6c7c696c914d101d48a13d4a4e74c04bdc998c1e84e5484660a0b9d4a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6368696d6572612f726f7574696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/chimera/routing)

[![Build Status](https://camo.githubusercontent.com/d9d4ef64d79f6a3373fbe237a0cc6d3fe1ddbe7ab93fb8030c90ac13c7cd7071/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6368696d6572617068702f726f7574696e672f706870756e69742e796d6c3f6272616e63683d312e302e78267374796c653d666c61742d737175617265)](https://github.com/chimeraphp/routing/actions?query=workflow%3A%22PHPUnit%20Tests%22+branch%3A1.0.x)[![Code Coverage](https://camo.githubusercontent.com/ada013539f1d8099e0ac665b27a21d24ff589f2c62899efd32b58b39677691f9/68747470733a2f2f636f6465636f762e696f2f67682f6368696d6572617068702f726f7574696e672f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/chimeraphp/routing)

> The term Chimera (*/kɪˈmɪərə/* or */kaɪˈmɪərə/*) has come to describe any mythical or fictional animal with parts taken from various animals, or to describe anything composed of very disparate parts, or perceived as wildly imaginative, implausible, or dazzling.

There are many many amazing libraries in the PHP community and with the creation and adoption of the PSRs we don't necessarily need to rely on full stack frameworks to create a complex and well designed software. Choosing which components to use and plugging them together can sometimes be a little challenging.

The goal of this set of packages is to make it easier to do that (without compromising the quality), allowing you to focus on the behaviour of your software.

This particular package provides PSR-15 **middleware** and reusable **request handlers** that help you to expose command and query handlers using HTTP as the web mechanism.

Installation
------------

[](#installation)

You probably won't depend directly on this package, but it is available on [Packagist](http://packagist.org/packages/chimera/routing), and can be installed it using [Composer](http://getcomposer.org):

```
composer require chimera/routing
```

### PHP Configuration

[](#php-configuration)

In order to make sure that we're dealing with the correct data, we're using `assert()`, which is a very interesting feature in PHP but not often used. The nice thing about `assert()` is that we can (and should) disable it in production mode so that we don't have useless statements.

So, for production mode, we recommend you to set `zend.assertions` to `-1` in your `php.ini`. For development you should leave `zend.assertions` as `1` and set `assert.exception` to `1`, which will make PHP throw an [`AssertionError`](https://secure.php.net/manual/en/class.assertionerror.php)when things go wrong.

Check the documentation for more information:

Components
----------

[](#components)

### Extension points

[](#extension-points)

The packages that extend this library should implement two basic interfaces, they're used to abstract how each routing library works:

- `Chimera\Routing\RouteParamsExtractor`: returns the list of parameters of the matched route
- `Chimera\Routing\UriGenerator`: generate routes based on the given arguments

### Route parameters extraction middleware

[](#route-parameters-extraction-middleware)

This middleware uses an implementation of `Chimera\Routing\RouteParamsExtractor`to put the parameters of the matched route in a standard attribute, so that other components can retrieve them.

### Request handlers

[](#request-handlers)

- `Chimera\Handler\CreateAndFetch`: executes a command to create a resource and immediately a query, returning an unformatted response with the query result and location header - intended to be used to handle **POST**requests
- `Chimera\Handler\CreateOnly`: executes a command to create a resource, returning an empty response with the location header - intended to be used to handle **POST** requests (variation of the previous one but can also be used in asynchronous APIs)
- `Chimera\Handler\ExecuteAndFetch`: executes a command to modify a resource and immediately a query, returning an unformatted response with the query result - intended to be used to handle **PUT** or **PATCH** requests
- `Chimera\Handler\ExecuteOnly`: executes a command to modify or remove a resource, returning an empty response - intended to be used to handle **PUT**, **PATCH**, or **DELETE** requests (can also be used in asynchronous APIs)
- `Chimera\Handler\FetchOnly`: executes a query to fetch a resource, returning an unformatted response with the query result - intended to be used to handle **GET** requests

Usage
-----

[](#usage)

### Middleware pipeline

[](#middleware-pipeline)

As mentioned above content negotiation is not a responsibility of the request handlers. It's expected that you configure [`lcobucci/content-negotiation-middleware`](https://github.com/lcobucci/content-negotiation-middleware)to process such task and it should be put in the very beginning of the pipeline, so that it can process **any** unformatted response.

The `Chimera\Routing\RouteParamsExtractor` middleware should be put right after the middleware responsible for matching routes (which changes for each implementation).

So a middleware pipeline in a [Zend Expressive v3 application](https://github.com/zendframework/zend-expressive-skeleton/blob/3.0.6/config/pipeline.php)would look like this - considering that all services are properly configured in the DI container:

```
