2017-09-04 84 views

回答

0

假设你是在一个控制器

$something = Something::findFirst($id); 
if (!$something) { 
    return $this->response->redirect('/404'); 
} 

,或者您可以使用调度

$this->dispatcher->forward(
    [ 
     'controller' => 'utils', 
     'action'  => 'notfound', 
    ] 
); 
+0

的URL将变为'/ 404'这不是我想要的。 – XieWilliam

+0

试试调度器上的'forward'方法。我改变了我的评论,告诉你如何 –

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