当id不存在时,我想回复404页面给客户。
$something = Something::findFirst($id);
if (!$something) {
//response a 404 page
}
怎么做?
答案 0 :(得分:0)
假设您在控制器中
$something = Something::findFirst($id);
if (!$something) {
return $this->response->redirect('/404');
}
或者您可以使用调度程序转发到其他操作
$this->dispatcher->forward(
[
'controller' => 'utils',
'action' => 'notfound',
]
);
答案 1 :(得分:0)
您想使用Dispatcher类中的forward()
方法。
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();
}
有类似问题的提问:Redirecting to 404 route in PhalconPHP results in Blank Page