@@ -7,12 +7,12 @@ Exposing Content via REST
7
7
8
8
Many applications need to expose content via REST APIs to partners or to
9
9
enable integration into other applications. As the CMF is build on top
10
- of Symfony2, its possible to leverage many of the available Bundles to
10
+ of Symfony2, it's possible to leverage many of the available bundles to
11
11
provide a REST API for content stored in the CMF. This cookbook will
12
- detail how to provide a read API using the following Bundles :
12
+ detail how to provide a read API using the following bundles :
13
13
14
- * :doc: `../bundles/content/index `;
15
- * `FOSRestBundle `_.
14
+ * :doc: `Cmf ContentBundle < ../bundles/content/index > `;
15
+ * `FOSRestBundle `_;
16
16
* `JMSSerializerBundle `_.
17
17
18
18
It is assumed that you have:
@@ -23,9 +23,9 @@ It is assumed that you have:
23
23
Installation
24
24
------------
25
25
26
- The ContentBundle provides support for the `FOSRestBundle view layer `_
26
+ The ContentBundle provides support for the `FOSRestBundle view layer `_,
27
27
which is automatically activated by the ``ContentController `` when the
28
- Bundle is available. Furthermore, the FOSRestBundle needs a serializer
28
+ bundle is available. Furthermore, the FOSRestBundle needs a serializer
29
29
to generate the REST output. The best choice is the JMSSerializerBundle:
30
30
31
31
.. code-block :: javascript
@@ -41,64 +41,127 @@ to generate the REST output. The best choice is the JMSSerializerBundle:
41
41
42
42
.. note ::
43
43
44
- Both Bundles are already required by the CreateBundle.
44
+ Both bundles are already required by the CreateBundle.
45
45
46
- .. note ::
46
+ .. caution ::
47
47
48
48
When using PHPCR-ODM it is necessary to require at least version 1.0.1
49
49
of ``doctrine\phpcr-odm ``.
50
50
51
- Then use composer to update your projects vendors:
51
+ Then use Composer _ to update your projects vendors:
52
52
53
53
.. code-block :: bash
54
54
55
- php composer.phar update
55
+ $ php composer.phar update
56
56
57
57
Configuring FOSRestBundle
58
58
-------------------------
59
59
60
60
Here is an example configuration for the FOSRestBundle.
61
61
62
- .. code-block :: jinja
63
-
64
- fos_rest:
65
- # configure the view handler
66
- view:
67
- force_redirects:
68
- html: true
69
- formats:
70
- json: true
71
- xml: true
72
- templating_formats:
73
- html: true
74
- # add a content negotiation rule, enabling support for json/xml for the entire website
75
- format_listener:
76
- rules:
77
- - { path: ^/, priorities: [ html, json, xml ], fallback_format: html, prefer_extension: false }
62
+ .. configuration-block ::
63
+
64
+ .. code-block :: yaml
65
+
66
+ fos_rest :
67
+ # configure the view handler
68
+ view :
69
+ force_redirects :
70
+ html : true
71
+ formats :
72
+ json : true
73
+ xml : true
74
+ templating_formats :
75
+ html : true
76
+ # add a content negotiation rule, enabling support for json/xml for the entire website
77
+ format_listener :
78
+ rules :
79
+ - { path: ^/, priorities: [ html, json, xml ], fallback_format: html, prefer_extension: false }
80
+
81
+ .. code-block :: xml
82
+
83
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
84
+ <container xmlns =" http://symfony.com/schema/dic/services" >
85
+
86
+ <config xmlns =" http://example.org/schema/dic/fos_rest" >
87
+ <!-- configure the view handler -->
88
+ <view >
89
+ <force-redirect name =" html" >true</force-redirect >
90
+
91
+ <format name =" json" >true</format >
92
+ <format name =" xml" >true</format >
93
+
94
+ <templating-format name =" html" >true</templating-format >
95
+ </view >
96
+
97
+ <!-- add a content negotiation rule, enabling support for json/xml for the entire website -->
98
+ <format-listener >
99
+ <rule path =" ^/"
100
+ fallback-format =" html"
101
+ prefer-extension =" false"
102
+ priorities =" html,json,xml"
103
+ />
104
+ </format-listener >
105
+ </config >
106
+ </container >
107
+
108
+ .. code-block :: php
109
+
110
+ $container->loadFromExtension('fos_rest', array(
111
+ // configure the view handler
112
+ 'view' => array(
113
+ 'force_redirects' => array(
114
+ 'html' => true,
115
+ ),
116
+ 'formats' => array(
117
+ 'json' => true,
118
+ 'xml' => true,
119
+ ),
120
+ 'templating_formats' => array(
121
+ 'html' => true,
122
+ ),
123
+ ),
124
+ // add a content negotiation rule, enabling support for json/xml for the entire website
125
+ 'format_listener' => array(
126
+ 'rules' => array(
127
+ array(
128
+ 'path' => '^/',
129
+ 'priorities' => array('html', 'json', 'xml'),
130
+ 'fallback_format' => 'html',
131
+ 'prefer_extension' => false,
132
+ ),
133
+ ),
134
+ ),
135
+ ));
78
136
79
137
Using the REST API
80
138
------------------
81
139
82
- This all it takes to enable read support via JSON or XML.
140
+ After you configured the FOSRestBundle, you need to execute the following
141
+ commands:
83
142
84
143
.. code-block :: bash
85
144
86
145
curl http://my-cmf.org/app_dev.php -H Accept:application/json
87
146
curl http://my-cmf.org/app_dev.php -H Accept:application/xml
88
147
curl http://my-cmf.org/app_dev.php -H Accept:text/html
89
148
149
+ This is all it takes to enable read support via JSON or XML!
150
+
90
151
The JMS serializer comes with sense defaults for Doctrine object mappers.
91
152
However it might be necessary to add additional mapping to more tightly
92
153
control what gets exposed. See the `documentation of the JMS serializer `_
93
154
for details.
94
155
95
156
.. note ::
96
157
97
- The `default response format changed between 1.0 and 1.1 of the ContentBundle `_. In
98
- 1.0 the response is wrapped inside an array/tag. This is no longer the case in 1.1
158
+ The `default response format changed between 1.0 and 1.1 of the ContentBundle `_.
159
+ In 1.0 the response is wrapped inside an array/tag. This is no longer the
160
+ case in 1.1
99
161
100
162
.. _`FOSRestBundle` : https://github.com/FriendsOfSymfony/FOSRestBundle
101
163
.. _`JMSSerializerBundle` : https://github.com/schmittjoh/JMSSerializerBundle
102
164
.. _`FOSRestBundle view layer` : https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/2-the-view-layer.md
165
+ .. _Composer : http://getcomposer.org/
103
166
.. _`documentation of the JMS serializer` : http://jmsyst.com/libs/#serializer
104
167
.. _`default response format changed between 1.0 and 1.1 of the ContentBundle` : https://github.com/symfony-cmf/ContentBundle/pull/91
0 commit comments