radnan/rdn-exception

Zend Framework 2 模块,用于标准化错误和异常

v1.3.2 2014-10-23 12:48 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:26:34 UTC


README

RdnException ZF2 模块将所有框架错误标准化为使用原生 PHP 异常。

模块包含两个处理 HTTP 响应的异常类

  • RdnException\AccessDeniedException - 抛出此异常将导致 403 响应。
  • RdnException\BadGatewayException - 抛出此异常将导致 502 响应。
  • RdnException\BadRequestException - 抛出此异常将导致 400 响应。
  • RdnException\MethodNotAllowedException - 抛出此异常将导致 405 响应。
  • RdnException\NotFoundException - 抛出此异常将导致 404 响应。
  • RdnException\UnauthorizedException - 抛出此异常将导致 401 响应。
  • RdnException\UnprocessableEntityException - 抛出此异常将导致 422 响应。

所有其他异常将导致 500 响应。

此外,还包含第三个类 RdnException\PublicException,用于在您希望向用户显示异常消息的情况下。所有 HTTP 异常都从此类扩展。

如何安装

  1. 使用 composer 要求 radnan/rdn-exception

    $ composer require radnan/rdn-exception:1.*
  2. 通过在您的 application.config.php 文件中包含它来激活模块

    <?php
    
    return array(
        'modules' => array(
            'RdnException',
            // ...
        ),
    );

如何使用

大多数配置都使用 rdn_exception 选项完成。

您可以根据需要自定义如果异常未显示则显示给用户的 消息。最后,您还可以控制用于异常类型的 模板

异常按其 HTTP 状态码排序。以下是一个示例配置

<?php

return array(
	'rdn_exception' => array(
		'messages' => array(
			'e403' => <<<HTML
<p>
	You are not authorized to access this page.
</p>
HTML
			, 'e404' => <<<HTML
<p>
	The page you're trying to reach doesn't exist.
</p>
HTML
			, 'e500' => <<<HTML
<h2>Report Error</h2>
<p>
	Please contact <a href="mailto:email@example.org">email@example.org</a> to report the problem.
</p>
HTML
		),

		'templates' => array(
			'e403' => 'rdn-exception/error/403',
			'e404' => 'rdn-exception/error/404',
			'e500' => 'rdn-exception/error/500',
		),
	),

	'view_manager' => array(
		'display_exceptions' => false,
	),
);