定制 yii1tech/error-handler 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yii1tech/error-handler

最新稳定版本:1.0.0

Composer 安装命令:

composer require yii1tech/error-handler

包简介

Provides Enhanced Error Handler for Yii1 application

README 文档

README

Yii1 Enhanced Error Handler


This extension provides Enhanced Error Handler for Yii1 application.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii1tech/error-handler 

or add

"yii1tech/error-handler": "*"

to the "require" section of your composer.json.

Usage

This extension provides Enhanced Error Handler for Yii1 application. Its main feature is conversion of the PHP errors into exceptions, so they may be processed via try..catch blocks.

Note: in order for error to exception conversion to work, the error handler component should be added to the application "preload" section.

Application configuration example:

<?php return [ 'preload' => [ 'errorHandler', // preload custom error handler, overriding the default one, allowing error to exception conversion // ... ], 'components' => [ 'errorHandler' => [ 'class' => \yii1tech\error\handler\ErrorHandler::class, ], // ... ], // ... ];

Once configured \yii1tech\error\handler\ErrorHandler allows you to catch PHP errors as exceptions. For example:

<?php try { // ... trigger_error('Some custom error message', E_USER_WARNING); // ... } catch (ErrorException $exception) { // handle error }

In addition, \yii1tech\error\handler\ErrorHandler provides support for error/exception rendering as JSON output, which is useful for modern XHR and API implementation. By default, the error will be rendered as JSON only in case the HTTP client passes header "Accept" with matching MIME type - "application/json". However, you may control this behavior using \yii1tech\error\handler\ErrorHandler::$shouldRenderErrorAsJsonCallback. For example:

<?php return [ 'preload' => [ 'errorHandler', // preload custom error handler, overriding the default one, allowing error to exception conversion // ... ], 'components' => [ 'errorHandler' => [ 'class' => \yii1tech\error\handler\ErrorHandler::class, 'shouldRenderErrorAsJsonCallback' => function () { if (!empty($_SERVER['HTTP_ACCEPT']) && strcasecmp($_SERVER['HTTP_ACCEPT'], 'application/json') === 0) { return true; } if (Yii::app()->request->isAjaxRequest) { return true; } if (str_starts_with(Yii::app()->request->getPathInfo(), 'api/')) { return true; } return false; }, ], // ... ], // ... ];

You may reuse the error handler ability to render errors as JSON in your custom error action, which is specified via \CErrorHandler::$errorAction. For example:

<?php class SiteController extends CController { public function actionError(): void { /** @var \yii1tech\error\handler\ErrorHandler $errorHandler */ $errorHandler = Yii::app()->getErrorHandler(); if ($errorHandler->shouldRenderErrorAsJson()) { // render JSON error representation. $errorHandler->renderErrorAsJson(); return; } // Render HTML error representation: if ($error = $errorHandler->error) { $this->render('error', $error); } } }

统计信息

  • 总下载量: 4.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固