You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Copy file name to clipboardExpand all lines: doc/book/middleware.md
+43-1Lines changed: 43 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ a "controller" in the routing defaults, you provide "middleware":
27
27
```php
28
28
// Via configuration:
29
29
return [
30
-
'router' =>
30
+
'router' =>
31
31
'routes' => [
32
32
'home' => [
33
33
'type' => 'literal',
@@ -106,3 +106,45 @@ causing the application to short-circuit and return the response immediately.
106
106
You can, however, return arbitrary values. If you do, the result is pushed into
107
107
the `MvcEvent` as the event result, allowing later dispatch listeners to
108
108
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) {
0 commit comments