Zend 2: How to throw a 404 error from controller

2019-04-19 07:09发布

I want to throw a '404 Page not found' error from my controller. How can I do that?

I use Zend Framework 2 not 1.

3条回答
Bombasti
2楼-- · 2019-04-19 07:14

Just try with:

$this->getResponse()->setStatusCode(404);
return; 

in your controller's action method.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-19 07:26

In the correct ZF2 setup you should already have your 404 view, then in your controller action just use the following and the 404 is automatically handled for you:

   if($notTheCorrectSlugMatchOrWhatEver){
   return  $this->notFoundAction();
   }
查看更多
孤傲高冷的网名
4楼-- · 2019-04-19 07:35
class IndexController extends AbstractActionController
{
    public function previewAction ()
    {
        return $this->notFoundAction ();
    }
}
查看更多
登录 后发表回答