dphdh395195 2013-12-11 20:04
浏览 82
已采纳

Zend Framework 2:为错误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."

I have tried specifying the reason in the ViewModel, setting the reasonPhrase from the response object... nothing seems to work. 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.

  • 写回答

1条回答 默认 最新

  • doujupa7567 2013-12-12 10:57
    关注

    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.

    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.

    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

    <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

    'view_manager' => array(
        'error/application_error' => __DIR__ . '/../view/error/application_error.phtml',
    ),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化