Zend 2:如何从控制器抛出404错误

时间:2021-08-05 20:17:41

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

我想从控制器中抛出'404 Page not found'错误。我怎样才能做到这一点?

I use Zend Framework 2 not 1.

我使用的是Zend Framework 2而不是1。

3 个解决方案

#1


25  

Just try with:

试试:

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

in your controller's action method.

在你的控制器的动作方法中。

#2


30  

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

#3


0  

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:

在正确的ZF2设置中,您应该已经拥有404视图,然后在您的控制器操作中使用以下内容并自动为您处理404:

   if($notTheCorrectSlugMatchOrWhatEver){
   return  $this->notFoundAction();
   }

#1


25  

Just try with:

试试:

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

in your controller's action method.

在你的控制器的动作方法中。

#2


30  

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

#3


0  

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:

在正确的ZF2设置中,您应该已经拥有404视图,然后在您的控制器操作中使用以下内容并自动为您处理404:

   if($notTheCorrectSlugMatchOrWhatEver){
   return  $this->notFoundAction();
   }