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

Commit 979fe60

Browse files
committed
initial commit of a cookbook entry for how to setup REST read support
1 parent b22a31b commit 979fe60

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
.. index::
2+
single: Tutorial, REST
3+
single: ContentBundle
4+
5+
Exposing Content via REST
6+
=========================
7+
8+
Many applications need to expose content via REST APIs to partners or to
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
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:
13+
14+
* :doc:`../bundles/content/index`;
15+
* `FOSRestBundle`_.
16+
* `JMSSerializerBundle`_.
17+
18+
It is assumed that you have:
19+
20+
* A working knowledge of the Symfony2 CMF framework;
21+
* An application with the ContentBundle setup.
22+
23+
Installation
24+
------------
25+
26+
The ContentBundle provides support for the `FOSRestBundle view layer`_
27+
which is automatically activated by the ``ContentController`` when the
28+
Bundle is available. Furthermore, the FOSRestBundle needs a serializer
29+
to generate the REST output. The best choice is the JMSSerializerBundle:
30+
31+
.. code-block:: javascript
32+
33+
{
34+
..
35+
"require": {
36+
..
37+
"friendsofsymfony/rest-bundle": "1.0.*",
38+
"jms/serializer-bundle": "0.13.*",
39+
}
40+
}
41+
42+
.. note::
43+
44+
Both Bundles are already required by the CreateBundle.
45+
46+
.. note::
47+
48+
When using PHPCR-ODM it is necessary to require at least version 1.0.1
49+
of ``doctrine\phpcr-odm``.
50+
51+
Then use composer to update your projects vendors:
52+
53+
.. code-block:: bash
54+
55+
php composer.phar update
56+
57+
Configuring FOSRestBundle
58+
-------------------------
59+
60+
Here is an example configuration for the FOSRestBundle.
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 }
78+
79+
Using the REST API
80+
------------------
81+
82+
This all it takes to enable read support via JSON or XML.
83+
84+
.. code-block:: bash
85+
86+
curl http://my-cmf.org/app_dev.php -H Accept:application/json
87+
curl http://my-cmf.org/app_dev.php -H Accept:application/xml
88+
curl http://my-cmf.org/app_dev.php -H Accept:text/html
89+
90+
The JMS serializer comes with sense defaults for Doctrine object mappers.
91+
However it might be necessary to add additional mapping to more tightly
92+
control what gets exposed. See the `documentation of the JMS serializer`_
93+
for details.
94+
95+
.. note::
96+
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
99+
100+
.. _`FOSRestBundle`: https://github.com/FriendsOfSymfony/FOSRestBundle
101+
.. _`JMSSerializerBundle`: https://github.com/schmittjoh/JMSSerializerBundle
102+
.. _`FOSRestBundle view layer`: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/2-the-view-layer.md
103+
.. _`documentation of the JMS serializer`: http://jmsyst.com/libs/#serializer
104+
.. _`default response format changed between 1.0 and 1.1 of the ContentBundle`: https://github.com/symfony-cmf/ContentBundle/pull/91

cookbook/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ The Cookbook
1313
creating_cms_using_cmf_and_sonata
1414
using_blockbundle_and_contentbundle
1515
handling_multilang_documents
16+
exposing_content_via_rest
1617

1718
.. include:: map.rst.inc

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
* :doc:`creating_cms_using_cmf_and_sonata`
1414
* :doc:`using_blockbundle_and_contentbundle`
1515
* :doc:`handling_multilang_documents`
16+
* :doc:`exposing_content_via_rest`

0 commit comments

Comments
 (0)