使zend框架错误报告更加用户友好

时间:2012-07-05 05:24:14

标签: zend-framework

我是zendframework的新手。现在我面临错误报告的问题;当我犯了小错误时,它会向我显示一条很长的错误信息,比如

 Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in /var/www/html/workbench/zend/library/Zend/Controller/Dispatcher/Standard.php:248 Stack trace: #0 /var/www/html/workbench/lajeesh/zend/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 /var/www/html/workbench/lajeesh/zend/library/Zend/Controller/Front.php(212): Zend_Controller_Front->dispatch() #2 /var/www/html/workbench/sudeepc/zend/web_root/index.php(10): Zend_Controller_Front::run('../application/...') #3 {main} Next exception 'Zend_Controller_Exception' with message 'Invalid controller specified (error)#0 /var/www/html/workbench/lajeesh/zend/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 /var/www/html/workbench/lajeesh/zend/library/Zend/Controller/Front.ph in /var/www/html/workbench/lajeesh/zend/library/Zend/Controller/Plugin/Broker.php on line 336 
等等....所以我的问题是如何配置zend框架错误报告更加用户友好?

2 个答案:

答案 0 :(得分:3)

错误管理在以下文件中完成..您可以自定义您想要的方式 1.ErrorController 2.error / error.phtml

ErrorController.php

<?php

class ErrorController extends Zend_Controller_Action
{

        public function init()
        {
            $this->_helper->layout->setLayout('set your default layout here'); //so it will be much more nicer
        }

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');

        if (!$errors) {
            $this->view->message = 'You have reached the error page';
            return;
        }

        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->message = PackAssist_Locale::translate('Sorry, the page doesnt exists'); //This is a custom message
                break;
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->message = 'Application error'; // put any custom messages if you want
                break;
        }

        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->crit($this->view->message, $errors->exception);
        }

        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }

        $this->view->request   = $errors->request;
    }

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }


}

error.phtml

<div>
  <h1><?php echo $this->translate('An error occurred');?></h1>
  <h2><?php echo $this->message; //Base error message ?></h2>

  <?php if (isset($this->exception)): ?>

  <h3><?php echo $this->translate('Exception information');?>:</h3>
  <p>
      <b>Message:</b> <?php echo $this->exception->getMessage(); ?>
  </p>

  <h3>Stack trace:</h3>
  <pre><?php  echo $this->exception->getTraceAsString(); //You can hide this for the end user ?>
  </pre>

  <h3>Request Parameters:</h3>
  <pre><?php  echo var_export($this->request->getParams(), true); // This will bea an array of your requested params POST/GET variables,your controller and the current action that produced the error ?>
  </pre>

3.如果您想完全隐藏错误,请将这些行放在配置文件中

phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

答案 1 :(得分:1)

我做了什么: 在库/ Zend / Controller / Dispatcher / Standard.php中编辑

找到有的地方:

 try {
        $controller->dispatch($action);
    } catch (Exception $e) {
        // Clean output buffer on error
        $curObLevel = ob_get_level();
        if ($curObLevel > $obLevel) {
            do {
                ob_get_clean();
                $curObLevel = ob_get_level();
            } while ($curObLevel > $obLevel);
        }
        throw $e;
    }

它就在之后:

/**
     * Dispatch the method call
     */

并添加die($ e-&gt; getMessage());在catch块中: 你会得到:

  try {
        $controller->dispatch($action);
    } catch (Exception $e) {
        die($e->getMessage());
        // Clean output buffer on error
        $curObLevel = ob_get_level();
        if ($curObLevel > $obLevel) {
            do {
                ob_get_clean();
                $curObLevel = ob_get_level();
            } while ($curObLevel > $obLevel);
        }
        throw $e;
    }

然后Zend提供的消息会更明确。