Zend 2: How to throw a 404 error from controller

限于喜欢 提交于 2019-12-04 22:54:36

Just try with:

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

in your controller's action method.

class IndexController extends AbstractActionController
{
    public function previewAction ()
    {
        return $this->notFoundAction ();
    }
}

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();
   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!