-- Jurian Sluiman <
[hidden email]> wrote
(on Tuesday, 05 June 2012, 09:46 PM +0200):
> 2012/6/5 Jurian Sluiman <
[hidden email]>
>
> 2012/6/5 Matthew Weier O'Phinney <
[hidden email]>
>
> -- Jurian Sluiman <
[hidden email]> wrote
> (on Sunday, 03 June 2012, 11:35 PM +0200):
> > What's the best option to throw an exception in a controller and
> expect it to
> > result in a Status: 404 Not Found response header?
>
> Don't throw an exception. Simply grab the response object, and set a
> 404
> status code on it. Doing so will trigger the RouteNotFoundStrategy and
> allow displaying a 404 page. (You can do similarly with a 500 status
> code.)
>
>
> This is what I was looking for. It is not documented (as documentation
> suggests responses are bubbling up to application level [1]) but I'll look
> into this, thanks!
>
> [1]
http://packages.zendframework.com/docs/latest/manual/en/> zend.mvc.examples.html#
> zend.mvc.examples.controllers.accessing-the-request-and-response
>
>
> As far as I can see now the process you described is not possible Matthew. I
> updated zf2 to 88c4b4cc6325209d8eb4f8b8e2d45f459bb5679d and the response I
> return is immediately sent to the browser. This results in a Status: 404 Not
> Found header, but obviously in an empty response too.
>
> If I do not return the Response object, but only set its status code and throw
> an exception afterwards, the status code is corrected to a 500. While your
> suggested sounds good, its not implemented yet or does not work as expected.
> Any thoughts on this?
I said set the status code, not return the response. :)
That's really all there is to it -- I've used it recently in stuff I was
working up to demo for DPC, and also use it in a custom PageController
in my website.
Look in Zend\Mvc\View\RouteNotFoundListener and ExceptionListener --
they each check the response status code, and, if so, inject an
appropriate ViewModel into the layout.
If you _return_ the response, the MVC expects that it's complete, and
tries to return it immediately. That's why you should only set the
status code, but not return the response. I've done it simply like this:
$this->getResponse()->setStatusCode(404);
return;
--
Matthew Weier O'Phinney
Project Lead |
[hidden email]
Zend Framework |
http://framework.zend.com/PGP key:
http://framework.zend.com/zf-matthew-pgp-key.asc