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

Commit 2d9a33a

Browse files
committed
middleware in dispatch event doc moved to cookbook
1 parent b92e9da commit 2d9a33a

File tree

3 files changed

+43
-42
lines changed

3 files changed

+43
-42
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Using Middleware in "dispatch" Event
2+
3+
During Mvc Workflow, you can use the Middleware in 'dispatch' event by provide `Psr7Bridge`. For example, you have `AuthorizationMiddleware`:
4+
5+
```php
6+
namespace Application\Middleware;
7+
8+
class AuthorizationMiddleware
9+
{
10+
public function __invoke($request, $response, $next = null)
11+
{
12+
// handle authorization here...
13+
}
14+
}
15+
```
16+
17+
As the request and response in 'dispatch' event is a `Zend\Http` Request and Response object, we need the bridge to convert into PSR-7 Request and Response. To do that, you can do the following:
18+
19+
```php
20+
namespace Application;
21+
22+
use Application\Middleware\AuthorizationMiddleware;
23+
use Zend\Psr7Bridge\Psr7ServerRequest;
24+
use Zend\Psr7Bridge\Psr7Response;
25+
26+
class Module
27+
{
28+
public function onBootstrap($e)
29+
{
30+
$app = $e->getApplication();
31+
$eventManager = $app->getEventManager();
32+
$services = $app->getServiceManager();
33+
34+
$eventManager->attach('dispatch', function ($e) use ($services) {
35+
$request = Psr7ServerRequest::fromZend($e->getRequest());
36+
$response = Psr7Response::fromZend($e->getResponse());
37+
$result = ($services->get(AuthorizationMiddleware::class))($request, $response);
38+
}, 2);
39+
}
40+
}
41+
```

doc/book/middleware.md

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -106,45 +106,3 @@ causing the application to short-circuit and return the response immediately.
106106
You can, however, return arbitrary values. If you do, the result is pushed into
107107
the `MvcEvent` as the event result, allowing later dispatch listeners to
108108
manipulate the results.
109-
110-
## Using Middleware in 'dispatch' Event
111-
112-
During Mvc Workflow, you can use the Middleware in 'dispatch' event by provide `Psr7Bridge`. For example, you have `AuthorizationMiddleware`:
113-
114-
```php
115-
namespace Application\Middleware;
116-
117-
class AuthorizationMiddleware
118-
{
119-
public function __invoke($request, $response, $next = null)
120-
{
121-
// handle authorization here...
122-
}
123-
}
124-
```
125-
126-
As the request and response in 'dispatch' event is a `Zend\Http` Request and Response object, we need the bridge to convert into PSR-7 Request and Response. To do that, you can do the following:
127-
128-
```php
129-
namespace Application;
130-
131-
use Application\Middleware\AuthorizationMiddleware;
132-
use Zend\Psr7Bridge\Psr7ServerRequest;
133-
use Zend\Psr7Bridge\Psr7Response;
134-
135-
class Module
136-
{
137-
public function onBootstrap($e)
138-
{
139-
$app = $e->getApplication();
140-
$eventManager = $app->getEventManager();
141-
$services = $app->getServiceManager();
142-
143-
$eventManager->attach('dispatch', function ($e) use ($services) {
144-
$request = Psr7ServerRequest::fromZend($e->getRequest());
145-
$response = Psr7Response::fromZend($e->getResponse());
146-
$result = ($services->get(AuthorizationMiddleware::class))($request, $response);
147-
}, 2);
148-
}
149-
}
150-
```

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 in "dispatch" Event': cookbook/using-middleware-in-dispatch-event.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)