zoukankan      html  css  js  c++  java
  • (转)zend异常处理

    Zend Framework 中提供了好几种 MVC 异常处理方式,首先让我们回顾下:

     

    1.  默认的交由 Zend_Controller_Plugin_ErrorHandler 插件来处理。

     

    2.  通过 Zend_Controller_Front::throwExceptions(true) 来处理:

     

    $front->throwExceptions(true);
     
    try {
        $front->dispatch();
    } catch (Exception $e) {
        // handle exceptions yourself
    }

     

    3.  通过 Zend_Controller_Response_Abstract::renderExceptions(true) 来处理,但不常用。通常是在开发的环境下使用。

     

    4.  通过 Zend_Controller_Front::returnResponse(true) 来处理:

     

    $front->returnResponse(true);
    $response = $front->dispatch();
     
    if ($response->isException()) {
        $exceptions = $response->getException();
        // handle exceptions ...
    } else {
        $response->sendHeaders();
        $response->outputBody();
    }

     

    更详细的内容请参考手册中 MVC Exceptions 一章:

    http://framework.zend.com/manual/en/zend.controller.exceptions.html#zend.controller.exceptions.handling

     

    这里我主要介绍第4种方案,也是公认最灵活的方案。

     

    第一,我们需要正确配置 Zend_Controller_Front :

     

    $front = Zend_Controller_Front::getInstance();
    $front->setParam('noErrorHandler', true)
          ->throwExceptions(false)
          ->returnResponse(true);

     

    我们禁用了 ErrorHandler ,阻止程序中途抛出异常,而把所有异常通过 response 对象来作最后处理:

     

     

     

    $response = $front->dispatch();
     
    if ($response->isException()) {
        $exceptions = $response->getException();
        echo handleException($exceptions[0]);
    } else {
        echo $response;
    }
     
    function handleException(Exception $e)
    {
        switch ($e->getCode()) {
            case 500: {
                if (!include_once(PROJECT_ROOT . '/error/500.html')) {
                    @header("HTTP/1.x 500 Internal Server Error");
                    @header('Status: 500 Internal Server Error');
                }
                exit;
                break;
            }
     
            default: {
                if (!include_once(PROJECT_ROOT . '/error/404.html')) {
                    @header('HTTP/1.x 404 Not Found');
                    @header('Status: 404 Not Found');
                }
                exit;
                break;
    }

     

    最后,我们编写 404.html 及 500.html 页面的内容,这里仅举404为例,500的类似。注意尽量达到 SEO 标准:

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>kimbs.info - 404 error: Page not found</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="kimbs.info" />
    <meta name="keywords" content="kimbs.info" />
    <meta name="robots" content="*" />
    <style type="text/css">
    .STYLE1 {
        color:#0000FF; 
        font-weight:bold; 
        font-size:25px
        font-weight: bold;
    }
     
    .STYLE2{
        font-size:15px;
        line-height:25px
    }
    </style>
    </head>
    <body style="margin-top:100px;">
        <div style="margin:0 auto; 650px;">
            <p>
                <h3 class="STYLE1">很抱歉,您要访问的页面不存在。</h3>
                <h3 class="STYLE1">
                    Sorry, the page you are trying to access
                    does not exist or perhaps it was removed.
                </h3>
            </p>
     
            <br />
     
            <p>
                <h2 class="STYLE2">
                    1、请检查您输入的地址是否正确。(Please check your input)
                </h2>
            </p>
     
            <p>
                <h2 class="STYLE2">
                    2、通过 <a href="http://kimbs.info/">http://kimbs.info/</a> 首页进行浏览。
                    (Welcome back to home page
                    <a href="http://kimbs.info/">http://kimbs.info/)</a></h2>
            </p>
     
            <p>
                <h2 class="STYLE2">
                    3、感谢您使用本站,如有疑问请<a href="http://kimbs.info/contract">联系我</a>。
                    (Thank you for visiting and
                    <a href="http://kimbs.info/contract">contract me</a> if any question)
                </h2>
            </p>
        </div>
    </body>
    </html>

     

  • 相关阅读:
    进阶篇:3.1.1.3)DFM注塑-机械紧固
    进阶篇:3.1.1.2)DFM注塑-卡扣
    进阶篇:5.3)装配偏移
    基础篇:6.9)形位公差-检测方法Measurement
    高阶篇:4.1)QFD质量功能展开-总章
    基础篇:6.3)形位公差-要素 Feature
    基础篇:6.8)形位公差-公差带 Tolerance Zone
    感想篇:9)关于结构工程师的一种打开方式
    知识点篇:5)产品结构设计工程师的定义、职责及技能要求
    [洛谷P3386][题解][模板]二分图匹配
  • 原文地址:https://www.cnblogs.com/gbyukg/p/2457487.html
Copyright © 2011-2022 走看看