Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 110 lines (77 sloc) 3.28 kB
c576b5f @jwage first commit
jwage authored
1 # Doctrine 2 REST Server and Client
2
3 The Doctrine 2 REST server and client component is both an easy way to spin up
4 REST services for your Doctrine 2 entities as well as a way to work with REST
5 services via an ActiveRecord style implementation similiar to ActiveResource in
6 Ruby on Rails!
7
8 ## Introduction
9
10 The basic concept is simple, you have a REST service (http://api.people.com/person)
11 and you want to interact with it through a simple ActiveRecord style interface.
12
13 First we can retrieve a person:
14
15 $person = Person::find(1); // GET http://api.people.com/person/1.xml
16
17 Now we can change some properties of that person:
18
19 $person->setName('Jonathan H. Wage');
20
21 Once we're done we can simply save it and the appropriate REST call will be made:
22
23 $person->save(); // POST http://api.people.com/person/1.xml (name=Jonathan H. Wage)
24
25 ## Client
26
27 The REST client is an ActiveRecord style implementation for working with REST
28 services. All you need to do is define some PHP classes that are mapped to some
29 REST service on the web. Here is an example where we map a Person to
30 http://api.people.com/person:
31
32 <?php
33
34 namespace Entities;
35
36 use Doctrine\REST\Client\Entity;
37
38 class Person extends Entity
39 {
40 private $id;
41 private $name;
42
43 public static function configure(EntityConfiguration $entityConfiguration)
44 {
45 $entityConfiguration->setUrl('http://api.people.com');
46 $entityConfiguration->setName('person');
47 }
48
49 public function getId()
50 {
51 return $this->id;
52 }
53
54 public function setName($name)
55 {
56 $this->name = $name;
57 }
58
59 public function getName()
60 {
61 return $this->name;
62 }
63 }
64
65 Now when we perform some actions it will generate the appropriate REST request,
66 execute it, transform the response and hydrate the results to your PHP objects.
67
68 $person = new Person();
69 $person->setName('Jonathan H. Wage');
70 $person->save(); // PUT http://api.people.com/person.xml (name=Jonathan H. Wage)
71
72 We can retrieve that person again now:
73
74 $person = Person::find($person->getId()); // GET http://api.people.com/person/1.xml
75
76 Or you can retrieve all Person objects:
77
78 $persons = Person::findAll();
79
80 ## Server
81
82 The Doctrine 2 REST server allows you to easily expose your entities through some
83 REST services. This is the raw low level server and does not include any routing
84 or URL parsing so you would need to implement in some existing framework that
85 has routing like Symfony or Zend Framework.
86
87 All you need to do is create a new REST server instance and pass it the instance
88 of your EntityManager you want to expose the entities for and an array representing
89 the server request you want to process:
90
91 $request = array(
92 '_method' => 'get',
93 '_format' => 'xml',
94 '_entity' => 'user',
95 '_action' => 'get',
96 '_id' => 1
97 );
98
99 $server = new \Doctrine\REST\Server\Server($em, $request);
1725df8 @jwage Adding ability for server to go through ORM or DBAL by itself. Initia…
jwage authored
100 $server->setEntityAlias('Entities\User', 'user');
c576b5f @jwage first commit
jwage authored
101
102 $xml = $server->execute();
103
104 The above would retrieve the User with the id of 1 and return an XML document
105 like the following:
106
107 <user>
108 <id>1</id>
109 <username>jwage</username>
110 </user>
Something went wrong with that request. Please try again.