tystr/rest-orm 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tystr/rest-orm

最新稳定版本:v0.6.0

Composer 安装命令:

composer require tystr/rest-orm

包简介

A simple ORM-like library handling object persistence with a RESTful API.

README 文档

README

Build Status Test Coverage Packagist Downloads

Note: as of v0.6.0 this library depends on PHP 7.x.

A simple ORM-like package for handling object persistence using a RESTful API.

Installation

Install tystr/rest-orm with composer:

# composer.phar require tystr/rest-orm:~0.1

Configuration

For each of your models you need to add mapping configuration and set up a Repository instance.

Mapping

RestOrm provides 2 annotations which must be used on each model:

  • @Resource This configures the name of the rest resource to use when generating urls.
  • @Id This configures the property to use as the identifier.

Data is hydrated into the models using JMS Serializer. See the documentation here for information on how to add mapping for your models.

<?php

use Tystr\RestOrm\Annotation\Resource;
use Tystr\RestOrm\Annotation\Id;
use JMS\Serializer\Annotation\Type;

/**
 * @Resource("blogs")
 */
class Blog
{
    /**
     * @Id
     * @Type("integer")
     */
    protected $id;

    /**
     * @Type("string")
     */
    protected $title;

    /**
     * @Type("string")
     */
    protected $body;

    /**
     * @Type("datetime")
     */
    protected $lastModified;

    // ... Accessor methods
}

Configure the Repository

Now that your models are mapped, you need to configure a Tystr\RestOrm\Repository\Repository instance for each of your models.

First, configure the guzzle client that will be used to make request to your API:

// Configure any auth headers when instantiating the guzzle client. These will be passed in each request.
$headers = [
    'Authorization' => 'Token 23a65de8ea1f2b52defea12c0d7a9c11'
];
$client = new GuzzleHttp\Client(['headers' => $headers]);

Next, set up the RequestFactory:

$urlGenerator = new Tystr\RestOrm\UrlGenerator\StandardUrlGenerator('https://example.com/api');
$format = 'json'; // either 'json' or 'xml'

$requestFactory = new Tystr\RestOrm\Request\Factory($urlGenerator, $format);

Now instantiate a Response Mapper. RestOrm currently provides 2 types of response mappers:

  • Tystr\RestOrm\Response\StandardResponseMapper for basic JSON serialization/deserialization
  • Tystr\RestOrm\Response\HalResponseMapper for HAL-formatted APIs
$responseMapper = new Tystr\RestOrm\Response\StandardResponseMapper();

Finally, instantiate a Repository class for each of your models:

// Instantiate a repository.
$class = 'Your\Project\Blog\Post';
$postRepository = new Tystr\RestOrm\Repository\Repository($client, $requestFactory, $responseMapper, $class);

Usage

The Tystr\RestOrm\Repository\RepositoryInterface currently provides 4 basic methods for interacting with your models:

  • save($model)
  • findOneById($id)
  • findAll()
  • remove($model)

统计信息

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

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

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