Skip to content

Commit

Permalink
Updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Sep 22, 2017
1 parent 72a19ad commit 4c75e7c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ $middleware = [
//and this is processed in DEV
[ENV === 'DEV', new MiddlewareAdmin()],

//we can create more custom matchers
[new RequestIsHttps(), new MiddlewareHttps()]
//we can use callables to use middlewares only in some conditions
[
function ($request) {
return $request->getUri()->getScheme() === 'https';
},
new MiddlewareHttps()
],

//or use the provided matchers
[new Pattern('*.png'), new MiddlewareForPngFiles()],

//And use several for each middleware component
[ENV === 'DEV', new RequestIsHttps(), new MiddlewareHttps()],
[ENV === 'DEV', new Pattern('*.png'), new MiddlewareForPngFilesInDev()],
];

$dispatcher = new Dispatcher($middleware, new Container());
Expand All @@ -60,7 +68,7 @@ $response = $dispatcher->dispatch(new Request());

## Matchers

As you can see in the example above, you can use an array of "matchers" to filter the requests that receive middlewares. You can use instances of `Middleland\Matchers\MatcherInterface` or booleans, but for comodity, the string values are also used to create `Middleland\Matchers\Path` instances. The available matchers are:
As you can see in the example above, you can use an array of "matchers" to filter the requests that receive middlewares. You can use callables, instances of `Middleland\Matchers\MatcherInterface` or booleans, but for comodity, the string values are also used to create `Middleland\Matchers\Path` instances. The available matchers are:

Name | Description | Example
-----|-------------|--------
Expand All @@ -70,15 +78,15 @@ Name | Description | Example

## How to create matchers

Just use the `Middleland\Matchers\MatcherInterface`. Example:
Just use a callable or an instance of the `Middleland\Matchers\MatcherInterface`. Example:

```php
use Middleland\Matchers\MatcherInterface;
use Psr\Http\Message\ServerRequestInterface;

class IsAjax implements MatcherInterface
{
public function match(ServerRequestInterface $request): bool
public function __invoke(ServerRequestInterface $request): bool
{
return $request->getHeaderLine('X-Requested-With') === 'xmlhttprequest';
}
Expand Down

0 comments on commit 4c75e7c

Please sign in to comment.