0
当id不存在时,我想响应一个404页面给客户端。如何在phalcon中响应404页面?
$something = Something::findFirst($id);
if (!$something) {
//response a 404 page
}
怎么办?
当id不存在时,我想响应一个404页面给客户端。如何在phalcon中响应404页面?
$something = Something::findFirst($id);
if (!$something) {
//response a 404 page
}
怎么办?
假设你是在一个控制器
$something = Something::findFirst($id);
if (!$something) {
return $this->response->redirect('/404');
}
,或者您可以使用调度
$this->dispatcher->forward(
[
'controller' => 'utils',
'action' => 'notfound',
]
);
您想使用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
的URL将变为'/ 404'这不是我想要的。 – XieWilliam
试试调度器上的'forward'方法。我改变了我的评论,告诉你如何 –