2

我是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 框架错误报告更用户友好?

4

2 回答 2

3

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

错误控制器.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;
    }


}

错误.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

于 2012-07-05T05:33:48.183 回答
1

我做了什么:在 library/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->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 传递的消息会更加明确。

于 2012-07-05T10:23:35.733 回答