Zend 2: How to throw a 404 error from controller

Deadly 提交于 2019-12-10 01:23:56

问题


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.


回答1:


Just try with:

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

in your controller's action method.




回答2:


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



回答3:


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();
   }


来源:https://stackoverflow.com/questions/15968331/zend-2-how-to-throw-a-404-error-from-controller

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