When id is not exists,I want to response a 404 page to client.
$something = Something::findFirst($id);
if (!$something) {
//response a 404 page
}
How to do it?
When id is not exists,I want to response a 404 page to client.
$something = Something::findFirst($id);
if (!$something) {
//response a 404 page
}
How to do it?
Assuming you are in a controller
$something = Something::findFirst($id);
if (!$something) {
return $this->response->redirect('/404');
}
or you can forward to a different action using the dispatcher
$this->dispatcher->forward(
[
'controller' => 'utils',
'action' => 'notfound',
]
);
You want to use forward()
method from Dispatcher class.
if ($somethingFailed) {
return $this->dispatcher->forward(['controller' => 'index', 'action' => 'error404']);
}
// Controller method
function error404Action()
{
$this->response->setStatusCode(404, "Not Found");
$this->view->pick('_layouts/error-404');
$this->response->send();
}
Question with similar problem: Redirecting to 404 route in PhalconPHP results in Blank Page