Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 6231329

Browse files
committed
Merge branch 'hotfix/94'
Close #94
2 parents c35b9fa + 4256067 commit 6231329

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#94](https://github.com/zendframework/zend-mvc/pull/94) adds a documentation
10+
recipe for using middleware withing MVC event listeners.
1011

1112
### Deprecated
1213

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Using middleware within event listeners
2+
3+
Within the MVC workflow, you can use middleware within event listeners by
4+
converting the request and response objects composed in the event to PSR-7
5+
equivalents using [zend-psr7bridge](https://github.com/zendframework/zend-psr7bridge).
6+
7+
As an example, consider the following `AuthorizationMiddleware`:
8+
9+
```php
10+
namespace Application\Middleware;
11+
12+
use Psr\Http\Message\ServerRequestInterface as RequestInterface;
13+
use Psr\Http\Message\ResponseInterface;
14+
15+
class AuthorizationMiddleware
16+
{
17+
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next = null)
18+
{
19+
// handle authorization here...
20+
}
21+
}
22+
```
23+
24+
Since the request and response composed in `MvcEvent` instances are specifically
25+
from zend-http, we will use zend-psr7bridge to convert them to PSR-7
26+
equivalents. As an example, consider the following module declaration, which
27+
registers a `dispatch` listener to invoke the above middleware:
28+
29+
```php
30+
namespace Application;
31+
32+
use Psr\Http\Message\ResponseInterface;
33+
use Zend\Psr7Bridge\Psr7ServerRequest;
34+
use Zend\Psr7Bridge\Psr7Response;
35+
36+
class Module
37+
{
38+
public function onBootstrap($e)
39+
{
40+
$app = $e->getApplication();
41+
$eventManager = $app->getEventManager();
42+
$services = $app->getServiceManager();
43+
44+
$eventManager->attach($e::EVENT_DISPATCH, function ($e) use ($services) {
45+
$request = Psr7ServerRequest::fromZend($e->getRequest());
46+
$response = Psr7Response::fromZend($e->getResponse());
47+
$done = function ($request, $response) {
48+
};
49+
50+
$result = ($services->get(Middleware\AuthorizationMiddleware::class))(
51+
$request,
52+
$response,
53+
$done
54+
);
55+
56+
if ($result) {
57+
return Psr7Response::toZend($result);
58+
}
59+
}, 2);
60+
}
61+
}
62+
```

doc/book/middleware.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ a "controller" in the routing defaults, you provide "middleware":
2727
```php
2828
// Via configuration:
2929
return [
30-
'router' =>
30+
'router' =>
3131
'routes' => [
3232
'home' => [
3333
'type' => 'literal',

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pages:
1414
- Examples: examples.md
1515
- 'Dispatching PSR-7 Middleware': middleware.md
1616
- 'Migration Guide': migration.md
17+
- Cookbook:
18+
- 'Using middleware within event listeners': cookbook/middleware-in-listeners.md
1719
site_name: zend-mvc
1820
site_description: 'zend-mvc: MVC application provider'
1921
repo_url: 'https://github.com/zendframework/zend-mvc'

0 commit comments

Comments
 (0)