定制 fojuth/response-exception 二次开发

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

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

fojuth/response-exception

Composer 安装命令:

composer require fojuth/response-exception

包简介

Response exception for Laravel4

README 文档

README

Response exception for Laravel4 is.. exception which returns response.

Install notes

Add desmart/response-exception as a requirement to composer.json:

{
  "require": {
    "desmart/response-exception": "1.1.*"
  }
}

Update your packages with composer update or install with composer install.

To add error handler add 'DeSmart\ResponseException\ResponseExceptionServiceProvider', to providers in app/config/app.php.

Other way of adding the handler is to add App::error() in app/filters.php:

App::error(DeSmart\ResponseException\ErrorHandlerFactory::make());

Just remember to add it after the registration of other App::error() handlers.

Why ?

Let's take a look at desmart/laravel-layout. It's a complex controller calling many actions.

Doing redirects in one of the actions is quite painful. That's because each action returns only a part of bigger response. The only way to return a response which will overwrite the big part is throwing of exception.

That's not the best design (exceptions aren't for that!), but it works and gives a chance to generate complete response from the smallest part of application.

Use cases

This package is useless for standard Laravel applications. Probably it fit's best with desmart/laravel-layout since it's a wicked controller.

Also in some edge cases it can be used for situations when some part of application needs to send own response during controller dispatch.

Examples

use DeSmart\ResponseException\Exception as ResponseException;

ResponseException::make('foo'); // sends 'foo' response

// There's an option to make chained exceptions
ResponseException::chain(Redirect::to('/'))
  ->withInput()
  ->fire();

The hack

In Laravel response may by returned in many parts of request cycle:

  • before routing (App::before())
  • during route dispatch
  • when exception is thrown (App::error)

Our hack is using exceptions to take advantage of App::error(). When error handler returns a value that value is treated as a response and is returned to client.

Since throwing an exception breaks normal code execution it's the best way to deliver fast a response.

There's a catch. Every exception is logged to file. The only way to get pass it is to register error handler as the last one. Laravel puts every error handler on top of handlers stack. When one of them returns response the others are not called.

统计信息

  • 总下载量: 61
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固