承接 nojimage/cakephp-rest 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

nojimage/cakephp-rest

Composer 安装命令:

composer require nojimage/cakephp-rest

包简介

A CakePHP Plugin containing a DataSource for interacting with ReSTful web services.

README 文档

README

A CakePHP Plugin containing a DataSource for interarcting with ReSTful web services.

Build Status

Requirements

  • PHP 5.2.8 upper
  • CakePHP 2.x.

Installation

Get plugin

using composer

composer require "nojimage/cakephp-rest":"2.*"

or, use git submodule

git submodule add https://github.com/nojimage/CakePHP-ReST-DataSource-Plugin.git app/Plugin/Rest

Setup APP/Config/bootstrap.php

CakePlugin::load('Rest');

How it works

  • The DataSource issues HTTP requests through CakePHP's HttpSocket class (default) or your own implementation/extension, e.g. an OAuth enabled HttpSocket http://github.com/neilcrookes/http_socket_oauth, passed in in the constructor.
  • It implements create, read, update and delete methods, which are called by Model::save(), Model::find() and Model::delete(). These methods expect your Model object to have a request property in the format defined in HttpSocket::request and add the appropriate HTTP verbs into the array if not already set, e.g. POST, GET, PUT, DELETE.
  • These methods call the public RestSource::request() method passing it an instance of your model object, but you can also access it directly, passing an object with a request property, or an arrayin the format defined in HttpSocket::request or a string which is a URI.
  • The raw response from the HttpSocket::request() is converted into an array, according to the response's content-type header (XML and JSON currently supported), and the result returned on success (determined by 2xx HTTP response code) or boolean false is returned on failure.
  • In addition, if the argument to the request() method was an object, the result is also added to a response property of the object, and if the request failed, and the object has an onError() method, that method will be triggered.

Direct Usage

  1. In APP/Config/database.php, append to the datasource definition.

    /**

    • RestSource
    • @var array */ public $rest = array( 'datasource' => 'Rest.RestSource', 'database' => false, );
  2. Create a model for each thing on web service you want to interact with. For example create a TwitterStatus model

  3. Ensure your model uses this datasource by setting it in the $useDbConfig property, e.g.

    public $useDbConfig = 'rest';

  4. Create a method in you model that corresponds to a function you can perform through the web service. E.g.

    public function save($data = null, $validate = true, $fieldList = array()) {
      $this->request = array(
        'uri' => array(
          'host' => 'twitter.com',
          'path' => 'statuses/update.json'
        ),
        'body' => array(
          'status' => $data['TwitterStatus']['text']
        )
      );
      return parent::save($data, $validate, $fieldList);
    }
    
  5. Anywhere in your application, call:

    $result = ClassRegistry::init('TwitterStatus')->save(array('TwitterStatus' => array('text' => 'Hello World!')));
    

N.B. This example is purely representative and does not include authentication for example, but demonstrate the general idea.

Other Uses

I abstracted this functionality out of a lot of other datasources I was writing and I use this extensively as a base data source which I extend for each web service I need, handling the specifics of each of those web services, such as Authentication, in those classes, by overloading each of the public methods in the Rest datasource as required.

For example, you can create a TwitterSource which extends RestSource and overload the constructor to pass in an instance of my HttpSocketOauth extension to CakePHP's HttpSocket class, and the request() method, adding in the common request params such as the host key and '.json' on the end of the path key, and the specific authentication params, then just call parent::request() to handle the issuing of the request and parsing of the response.

nojimage/cakephp-rest 适用场景与选型建议

nojimage/cakephp-rest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 716 次下载、GitHub Stars 达 13, 最近一次更新时间为 2013 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 nojimage/cakephp-rest 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 13
  • Watchers: 5
  • Forks: 26
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-02-09