Doctrine 2 REST

Doctrine REST服务器和客户端库。(Doctrine REST Server and Client Library.)

  • Owner: doctrine/rest
  • Platform: BSD, Linux, Mac, Windows
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

这个项目没有维护

Doctrine 2 REST服务器和客户端

Doctrine 2 REST服务器和客户端组件既是为Doctrine 2实体启用REST服务的简单方法,也是通过类似于Ruby on Rails中的ActiveResource的ActiveRecord样式实现来使用REST服务的一种方式!

介绍

基本概念很简单,你有一个REST服务(http://api.people.com/person),你想通过一个简单的ActiveRecord样式界面与它进行交互。

首先,我们可以检索一个人:

$person = Person::find(1); // GET http://api.people.com/person/1.xml

现在我们可以改变该人的一些属性:

$person->setName('Jonathan H. Wage');

一旦我们完成,我们可以简单地保存它,并进行适当的REST调用:

$person->save(); // POST http://api.people.com/person/1.xml (name=Jonathan H. Wage)

客户端

REST客户端是用于处理REST服务的ActiveRecord样式实现。 您需要做的就是定义一些映射到Web上某些REST服务的PHP类。 这是我们将Person映射到的示例 http://api.people.com/person

<?php

namespace Entities;

use Doctrine\REST\Client\Entity;

class Person extends Entity
{
    private $id;
    private $name;

    public static function configure(EntityConfiguration $entityConfiguration)
    {
        $entityConfiguration->setUrl('http://api.people.com');
        $entityConfiguration->setName('person');
    }

    public function getId()
    {
        return $this->id;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }
}

现在,当我们执行某些操作时,它将生成相应的REST请求,执行它,转换响应并将结果保存到PHP对象。

$person = new Person();
$person->setName('Jonathan H. Wage');
$person->save(); // PUT http://api.people.com/person.xml (name=Jonathan H. Wage)

我们现在可以再次找回那个人:

$person = Person::find($person->getId()); // GET http://api.people.com/person/1.xml

或者您可以检索所有Person对象:

$persons = Person::findAll();

服务器

Doctrine 2 REST服务器允许您通过一些REST服务轻松公开您的实体。 这是原始的低级服务器,不包括任何路由或URL解析,因此您需要在具有Symfony或Zend Framework等路由的现有框架中实现。

您需要做的就是创建一个新的REST服务器实例,并将要为其公开实体的EntityManager实例和表示您要处理的服务器请求的数组传递给它:

$request = array(
    '_method' => 'get',
    '_format' => 'xml',
    '_entity' => 'user',
    '_action' => 'get',
    '_id' => 1
);

$server = new \Doctrine\REST\Server\Server($em, $request);
$server->setEntityAlias('Entities\User', 'user');

$xml = $server->execute();

上面的代码将检索ID为1的用户并返回一个XML文档 如下所示:

<user>
    <id>1</id>
    <username>jwage</username>
</user>

Main metrics

Overview
Name With Ownerdoctrine/rest
Primary LanguagePHP
Program languagePHP (Language Count: 3)
PlatformBSD, Linux, Mac, Windows
License:MIT License
所有者活动
Created At2010-04-05 17:23:31
Pushed At2015-03-29 19:27:19
Last Commit At2015-03-28 18:05:40
Release Count0
用户参与
Stargazers Count140
Watchers Count14
Fork Count29
Commits Count31
Has Issues Enabled
Issues Count1
Issue Open Count1
Pull Requests Count5
Pull Requests Open Count1
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

THIS PROJECT IS NOT MAINTAINED

Doctrine 2 REST Server and Client

The Doctrine 2 REST server and client component is both an easy way to spin up
REST services for your Doctrine 2 entities as well as a way to work with REST
services via an ActiveRecord style implementation similiar to ActiveResource in
Ruby on Rails!

Introduction

The basic concept is simple, you have a REST service (http://api.people.com/person)
and you want to interact with it through a simple ActiveRecord style interface.

First we can retrieve a person:

$person = Person::find(1); // GET http://api.people.com/person/1.xml

Now we can change some properties of that person:

$person->setName('Jonathan H. Wage');

Once we're done we can simply save it and the appropriate REST call will be made:

$person->save(); // POST http://api.people.com/person/1.xml (name=Jonathan H. Wage)

Client

The REST client is an ActiveRecord style implementation for working with REST
services. All you need to do is define some PHP classes that are mapped to some
REST service on the web. Here is an example where we map a Person to
http://api.people.com/person:

<?php

namespace Entities;

use Doctrine\REST\Client\Entity;

class Person extends Entity
{
    private $id;
    private $name;

    public static function configure(EntityConfiguration $entityConfiguration)
    {
        $entityConfiguration->setUrl('http://api.people.com');
        $entityConfiguration->setName('person');
    }

    public function getId()
    {
        return $this->id;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }
}

Now when we perform some actions it will generate the appropriate REST request,
execute it, transform the response and hydrate the results to your PHP objects.

$person = new Person();
$person->setName('Jonathan H. Wage');
$person->save(); // PUT http://api.people.com/person.xml (name=Jonathan H. Wage)

We can retrieve that person again now:

$person = Person::find($person->getId()); // GET http://api.people.com/person/1.xml

Or you can retrieve all Person objects:

$persons = Person::findAll();

Server

The Doctrine 2 REST server allows you to easily expose your entities through some
REST services. This is the raw low level server and does not include any routing
or URL parsing so you would need to implement in some existing framework that
has routing like Symfony or Zend Framework.

All you need to do is create a new REST server instance and pass it the instance
of your EntityManager you want to expose the entities for and an array representing
the server request you want to process:

$request = array(
    '_method' => 'get',
    '_format' => 'xml',
    '_entity' => 'user',
    '_action' => 'get',
    '_id' => 1
);

$server = new \Doctrine\REST\Server\Server($em, $request);
$server->setEntityAlias('Entities\User', 'user');

$xml = $server->execute();

The above would retrieve the User with the id of 1 and return an XML document
like the following:

<user>
    <id>1</id>
    <username>jwage</username>
</user>