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

add doc for middleware usage in "dispatch" event #94

Merged

Conversation

samsonasik
Copy link
Contributor

#92

@weierophinney
Copy link
Member

@samsonasik Please create a new cookbook/ subtree in the documentation source, and add this. Then, in mkdocs.yml, create a Cookbook subsection, with this as the only sub-document.

Thanks!

@samsonasik
Copy link
Contributor Author

@weierophinney done ;). moved to cookbook and updated mkdocs.yml

$eventManager->attach('dispatch', function ($e) use ($services) {
$request = Psr7ServerRequest::fromZend($e->getRequest());
$response = Psr7Response::fromZend($e->getResponse());
$result = ($services->get(AuthorizationMiddleware::class))($request, $response);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you could pass an empty closure as third parameter to the middleware, so that if the middleware return $next($req, $res), then $result will be null, and if the middleware return $response, then $result will be a response. In case it returns a response, you can convert it back to Zend\Http and return it (short-circuiting).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ;)

@danizord
Copy link
Contributor

danizord commented Mar 9, 2016

@samsonasik now in listener, you can add this:

if ($result instanceof \Psr\Http\Message\ResponseInterface) {
    return Psr7Response::toZend($result);
}

So that the middleware becomes able to short-circuit, for example some AuthorizationMiddleware can return a 403 response if not authorized. :)

@samsonasik
Copy link
Contributor Author

@danizord done ;) Thank you ;)

@weierophinney weierophinney merged commit fe66a64 into zendframework:master Apr 6, 2016
weierophinney added a commit that referenced this pull request Apr 6, 2016
add doc for middleware usage in "dispatch" event
weierophinney added a commit that referenced this pull request Apr 6, 2016
weierophinney added a commit that referenced this pull request Apr 6, 2016
weierophinney added a commit that referenced this pull request Apr 6, 2016
@weierophinney weierophinney added this to the 2.7.6 milestone Apr 6, 2016
@weierophinney weierophinney self-assigned this Apr 6, 2016
@weierophinney
Copy link
Member

Thanks, @samsonasik !

@samsonasik samsonasik deleted the doc/dispatch-middleware branch April 6, 2016 20:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants