简体   繁体   English

Zend Framework 2:设置错误404的原因短语

[英]Zend Framework 2 : set reason phrase for error 404

I want my controller to return a 404 response when a model is not found and I want to specify a custom message, not the default " The requested controller was unable to dispatch the request. " 我希望我的控制器在找不到模型并且要指定自定义消息而不是默认消息时返回404响应,而不是默认消息“ The requested controller was unable to dispatch the request.

I have tried specifying the reason in the ViewModel , setting the reasonPhrase from the response object... nothing seems to work. 我尝试在ViewModel指定reason ,从响应对象设置reasonPhrase ……似乎没有任何效果。 I am currently investigating how I can prevent the default behaviour, but if someone know before I do, that would just be great. 我目前正在研究如何防止默认行为,但是如果有人在我之前知道了,那就太好了。 (Perhaps there's a better way than the one I would find anyhow, too.) (也许比我也找到的方法更好。)

Here is what I have, which does not work : 这是我所拥有的,不起作用:

 $userModel = $this->getUserModel();
 if (empty($userModel)) {
     $this->response->setStatusCode(404);
     $this->response->setReasonPhrase('error-user-not-found');
     return new ViewModel(array(
         'content' => 'User not found',
     ));
 }

Thanks. 谢谢。

Looks like you are confusing the reasponphrase and the reason variable passed on to the view. 看起来您在混淆重新命名和将原因变量传递给视图。 The reasonphrase is part of the http status code, like "Not Found" for 404. You probably don't want to change that. 原因短语是http状态代码的一部分,例如404的“未找到”。您可能不想更改它。

Like @dphn is saying, I would recommend throwing your own Exception and attach a listener to the MvcEvent::EVENT_DISPATCH_ERROR which decides what to respond. 就像@dphn所说的那样,我建议您抛出自己的Exception并将侦听器附加到MvcEvent::EVENT_DISPATCH_ERROR ,该侦听器确定响应的内容。

To get you started: 为了帮助您入门:

Controller 调节器

public function someAction()
{
    throw new \Application\Exception\MyUserNotFoundException('This user does not exist');
}

Module

public function onBootstrap(MvcEvent $e)
{
    $events = $e->getApplication()->getEventManager();

    $events->attach(
        MvcEvent::EVENT_DISPATCH_ERROR,
        function(MvcEvent $e) {
            $exception = $e->getParam('exception');
            if (! $exception instanceof \Application\Exception\MyUserNotFoundException) {
                return;
            }

            $model = new ViewModel(array(
                'message' => $exception->getMessage(),
                'reason' => 'error-user-not-found',
                'exception' => $exception,
            ));
            $model->setTemplate('error/application_error');
            $e->getViewModel()->addChild($model);

            $response = $e->getResponse();
            $response->setStatusCode(404);

            $e->stopPropagation();

            return $model;
        },
        100
    );
}

error/application_error.phtml 错误/ application_error.phtml

<h1><?php echo 'A ' . $this->exception->getStatusCode() . ' error occurred ?></h1>
<h2><?php echo $this->message ?></h2>  
<?php
switch ($this->reason) {
    case 'error-user-not-found':
      $reasonMessage = 'User not found';
      break;
}
echo $reasonMessage;

module.config.php module.config.php

'view_manager' => array(
    'error/application_error' => __DIR__ . '/../view/error/application_error.phtml',
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM