0

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?

Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
XieWilliam
  • 167
  • 1
  • 14

2 Answers2

0

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',
    ]
);
Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67
0

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

Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32