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

Commit d673b12

Browse files
committed
Merge branch 'hotfix/94' into develop
Forward port #94
2 parents ae15c2c + 4256067 commit d673b12

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
@@ -101,7 +101,8 @@ for full details on how to migrate your v2 application.
101101

102102
### Added
103103

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

106107
### Deprecated
107108

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
@@ -17,6 +17,8 @@ pages:
1717
- "Migration Overview": migration/index.md
1818
- 'v2.X to v2.7': migration/to-v2-7.md
1919
- 'v2.X to v3.0': migration/to-v3-0.md
20+
- Cookbook:
21+
- 'Using middleware within event listeners': cookbook/middleware-in-listeners.md
2022
site_name: zend-mvc
2123
site_description: 'zend-mvc: MVC application provider'
2224
repo_url: 'https://github.com/zendframework/zend-mvc'

0 commit comments

Comments
 (0)