【问题标题】:How do you handle errors when using a service layer?使用服务层时如何处理错误?
【发布时间】:2011-05-15 12:55:20
【问题描述】:

在我的 Zend Framework 项目中,我使用了一个服务层,但是我真的不知道在哪里处理错误。

例如,假设我有一个UserService::updateUser($data);

如果我有:

$data = array(
   'userId' => 2,
   'firstName' => 'Jane',
   'lastName'  => 'Doe',
);

id 为 2 的用户不存在?

您将在哪里以及如何处理此类错误?

【问题讨论】:

    标签: php zend-framework exception-handling software-design service-layer


    【解决方案1】:

    您可以转发给特定的控制器来处理您的所有业务错误,如下所示:

    if ($error=true)
            return $this->_forward('standarderror', 'businesserror', 'default',
                    array('msgtitle' => $this->view->translate('item_not_found'),
                        'msg' => $this->view->translate('item_not_found_msg')));
    

    以及您的 BusinesserrorController 的样子:

    class BusinesserrorController extends Zend_Controller_Action {
    
    public function init() {
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function standarderrorAction() {
        $msgtitle = $this->_getParam('msgtitle');
        $msg = $this->_getParam('msg');
    
        $this->view->errortitle = $msgtitle;
        $this->view->errormessage = $msg;
    
        $this->view->nextstep = $this->view->translate('return_to_the_homepage');
        $this->view->nextstepurl = "/";
    
        echo $this->render('error/businesserror', null, true);
    }
    
    }
    

    您也可以参数化转发的 url ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 2014-07-18
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 2022-10-13
      相关资源
      最近更新 更多