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

Commit 4bd463e

Browse files
committed
Merge pull request #94 from samsonasik/doc/dispatch-middleware
add doc for middleware usage in "dispatch" event
2 parents c35b9fa + fe66a64 commit 4bd463e

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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, and then, the result of invoked `AuthorizationMiddleware` above needs to be converted to `Zend\Http` Response if an instance of `Psr\Http\Message\ResponseInterface`. To do that, you can do the following:
18+
19+
```php
20+
namespace Application;
21+
22+
use Application\Middleware\AuthorizationMiddleware;
23+
use Psr\Http\Message\ResponseInterface;
24+
use Zend\Psr7Bridge\Psr7ServerRequest;
25+
use Zend\Psr7Bridge\Psr7Response;
26+
27+
class Module
28+
{
29+
public function onBootstrap($e)
30+
{
31+
$app = $e->getApplication();
32+
$eventManager = $app->getEventManager();
33+
$services = $app->getServiceManager();
34+
35+
$eventManager->attach('dispatch', function ($e) use ($services) {
36+
$request = Psr7ServerRequest::fromZend($e->getRequest());
37+
$response = Psr7Response::fromZend($e->getResponse());
38+
$result = ($services->get(AuthorizationMiddleware::class))($request, $response, function() {});
39+
40+
if ($result instanceof ResponseInterface) {
41+
return Psr7Response::toZend($result);
42+
}
43+
}, 2);
44+
}
45+
}
46+
```

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 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)