定制 divspace/responder 二次开发

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

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

divspace/responder

Composer 安装命令:

composer require divspace/responder

包简介

A Laravel 4 wrapper for Fractal to handle API responses

README 文档

README

A Laravel 4 wrapper for Fractal to handle API responses. For Laravel 5, please go here.

Installation

Add Responder to your composer.json file by running the following command:

$ composer require divspace/responder

Then run the following command:

$ composer update

Register Package

Add the following line to the end of the providers array in app/config/app.php:

'providers' => array(
    // ...
    'Divspace\Responder\ResponderServiceProvider'
)

Usage

There are three types of responses you can return:

  1. Item
  2. Collection
  3. Paginated Collection

Here are examples of all three types in a single controller:

use Divspace\Responder\Responder;
use Data\Transformers\UserTransformer;

class UserController extends BaseController {
    protected $response;

    public function __construct(Responder $response) {
        $this->response = $response;

        /**
         * Fractal parseIncludes() method
         * http://fractal.thephpleague.com/transformers/
         */
        if(isset($_GET['include'])) {
            $this->response->parseIncludes($_GET['include']);
        }
    }

    /**
     * Display a paginated list of users
     */
    public function index() {
        return $this->response->paginatedCollection(User::paginate());
    }

    /**
     * Display a single user
     */
    public function item($id) {
        return $this->response->item(User::find($id), new UserTransformer());
    }

    /**
     * Display all users
     */
    public function collection() {
        return $this->response->collection(User::all(), new UserTransformer());
    }
}

The Data\Transformers\UserTransformers file might look something like this (with an example of how to use the parseIncludes() method from the previous example):

<?php namespace Data\Transformers;

use User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract {

    protected $defaultIncludes = [
        'user_type'
    ];

    public function transform(User $user) {
        $user = User::find($user->id);

        return [
            'id'         => (int) $user->id,
            'email'      => $user->email,
            'created_at' => $user->created_at,
            'updated_at' => $user->updated_at,
        ];
    }

    public function includeUserType(User $user) {
        $userType = $user->type;

        return $this->item($userType, new UserTypeTransformer);
    }

}

Since we've used the parseIncludes() method in the previous example, we need to create that file as well so we can return more information in our API response:

<?php namespace Data\Transformers;

use User;
use League\Fractal\TransformerAbstract;

class UserTypeTransformer extends TransformerAbstract {

    public function transform($userType) {
        if(count($userType) > 0) {
            return [
                'id'   => (int) $userType->id,
                'name' => $userType->name
            ];
        }

        return [];
    }

}

You'll want to refer to the full Fractal documentation to get an idea of how everything works together.

Credit

This is modified fork of Larasponse for Laravel 4.2 that uses Fractal as the default provider for API responses.

divspace/responder 适用场景与选型建议

divspace/responder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 156 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「rest」 「api」 「laravel」 「league」 「fractal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 divspace/responder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 divspace/responder 我们能提供哪些服务?
定制开发 / 二次开发

基于 divspace/responder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-23