Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 25a3d23

Browse files
committed
Minor fixes and added missing formats
1 parent 979fe60 commit 25a3d23

File tree

1 file changed

+92
-29
lines changed

1 file changed

+92
-29
lines changed

cookbook/exposing_content_via_rest.rst

Lines changed: 92 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Exposing Content via REST
77

88
Many applications need to expose content via REST APIs to partners or to
99
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
1111
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:
1313

14-
* :doc:`../bundles/content/index`;
15-
* `FOSRestBundle`_.
14+
* :doc:`Cmf ContentBundle <../bundles/content/index>`;
15+
* `FOSRestBundle`_;
1616
* `JMSSerializerBundle`_.
1717

1818
It is assumed that you have:
@@ -23,9 +23,9 @@ It is assumed that you have:
2323
Installation
2424
------------
2525

26-
The ContentBundle provides support for the `FOSRestBundle view layer`_
26+
The ContentBundle provides support for the `FOSRestBundle view layer`_,
2727
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
2929
to generate the REST output. The best choice is the JMSSerializerBundle:
3030

3131
.. code-block:: javascript
@@ -41,64 +41,127 @@ to generate the REST output. The best choice is the JMSSerializerBundle:
4141
4242
.. note::
4343

44-
Both Bundles are already required by the CreateBundle.
44+
Both bundles are already required by the CreateBundle.
4545

46-
.. note::
46+
.. caution::
4747

4848
When using PHPCR-ODM it is necessary to require at least version 1.0.1
4949
of ``doctrine\phpcr-odm``.
5050

51-
Then use composer to update your projects vendors:
51+
Then use Composer_ to update your projects vendors:
5252

5353
.. code-block:: bash
5454
55-
php composer.phar update
55+
$ php composer.phar update
5656
5757
Configuring FOSRestBundle
5858
-------------------------
5959

6060
Here is an example configuration for the FOSRestBundle.
6161

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+
));
78136
79137
Using the REST API
80138
------------------
81139

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:
83142

84143
.. code-block:: bash
85144
86145
curl http://my-cmf.org/app_dev.php -H Accept:application/json
87146
curl http://my-cmf.org/app_dev.php -H Accept:application/xml
88147
curl http://my-cmf.org/app_dev.php -H Accept:text/html
89148
149+
This is all it takes to enable read support via JSON or XML!
150+
90151
The JMS serializer comes with sense defaults for Doctrine object mappers.
91152
However it might be necessary to add additional mapping to more tightly
92153
control what gets exposed. See the `documentation of the JMS serializer`_
93154
for details.
94155

95156
.. note::
96157

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
99161

100162
.. _`FOSRestBundle`: https://github.com/FriendsOfSymfony/FOSRestBundle
101163
.. _`JMSSerializerBundle`: https://github.com/schmittjoh/JMSSerializerBundle
102164
.. _`FOSRestBundle view layer`: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/2-the-view-layer.md
165+
.. _Composer: http://getcomposer.org/
103166
.. _`documentation of the JMS serializer`: http://jmsyst.com/libs/#serializer
104167
.. _`default response format changed between 1.0 and 1.1 of the ContentBundle`: https://github.com/symfony-cmf/ContentBundle/pull/91

0 commit comments

Comments
 (0)