In my controller I throw a 404 response after an If statement, something like that :
if ($foo) {
$this->getResponse()->setStatusCode(404);
return;
}
Then, I would like to send some variables to my 404 page. In my mind, I want to do something like that :
$this->getResponse()->setVariables(array('foo' => 'bar', 'baz' => 'bop'));
$this->getResponse()->setStatusCode(404);
return;
It's not the good solution, so how I have to do that ?
And after, how to get these variables in my 404 view ?
Thank you
Oh god..
I was so dumb
Solution :
In 404.phtml :
I've come to this question from Google and my issue was a bit more difficult. Since 404 error could be thrown from absolutely unpredictable url, you can't be sure you catched it in some controller. Controller – is too late to catch 404 error.
The solution in my case was to catch an
EVENT_DISPATCH_ERROR
and totally rebuildviewModel
. Cavern is that layout – is a rootviewModel
, and content appended into layout by default is anotherviewModel
(child). These points are not such clear described in official docs.Here is how it can look like in your
Module.php
: