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

Commit 755674f

Browse files
committed
Updating for manual walkthrough.
1 parent 182c306 commit 755674f

File tree

7 files changed

+287
-189
lines changed

7 files changed

+287
-189
lines changed

cookbook/creating_a_cms/000-index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Creating a Basic CMS
88
010-getting-started
99
020-auto-routing
1010
030-sonata-admin
11+
035-content-to-controllers
1112
040-the-frontend
1213
050-make-homepage
1314
900-conclusion

cookbook/creating_a_cms/010-getting-started.rst

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Part 1 - Getting Started
2-
------------------------
1+
Getting Started
2+
---------------
33

44
Initializing the Project
55
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -10,14 +10,18 @@ create a new project using the PHPCR-ODM.
1010
Install Additional Bundles
1111
..........................
1212

13-
This tutorial requires the following packages:
13+
The complete tutorial requires the following packages:
1414

1515
* `symfony-cmf/routing-auto-bundle`_;
1616
* `sonata-project/doctrine-phpcr-admin-bundle`_;
1717
* `doctrine/data-fixtures`_;
1818
* `symfony-cmf/menu-bundle`_.
1919

20-
Update ``composer.json`` to require them:
20+
Each part of the tutorial will detail the packages that it requires (if any) in a
21+
section titled "installation".
22+
23+
If you intend to complete the entire tutorial you can save some time by adding
24+
all of the required packages now.
2125

2226
.. code-block:: javascript
2327
@@ -28,26 +32,24 @@ Update ``composer.json`` to require them:
2832
"symfony-cmf/routing-auto-bundle": "1.0.0@alpha",
2933
"symfony-cmf/menu-bundle": "1.0",
3034
"sonata-project/doctrine-phpcr-admin-bundle": "dev-master",
31-
"doctrine/data-fixtures": "1.0.0"
35+
"doctrine/data-fixtures": "1.0.0",
36+
37+
"doctrine/phpcr-odm": "dev-master as 1.0.0",
38+
"phpcr/phpcr-utils": "dev-master as 1.0.0",
39+
"doctrine/phpcr-bundle": "dev-master as 1.0.0"
3240
},
3341
...
3442
}
3543
36-
And add the packages to the kernel::
44+
.. note::
3745

38-
class AppKernel extends Kernel
39-
{
40-
public function registerBundles()
41-
{
42-
$bundles = array(
43-
// ...
44-
new Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(),
45-
new Symfony\Cmf\Bundle\RoutingAutoBundle\CmfRoutingAutoBundle(),
46-
);
46+
This tutorial currently requires code only available in the lastest
47+
unstable version of PHPCR-ODM, this is why you require the "dev-master as
48+
1.0.0" constraints. When PHPCR-ODM 1.1 is released this will no longer be
49+
necessary.
4750

48-
// ...
49-
}
50-
}
51+
Note that each time you modify your ``composer.json`` file you are required to
52+
run ``composer update``.
5153

5254
Initialize the Database
5355
.......................
@@ -323,9 +325,32 @@ reinitialize) the repository:
323325
however that it is the responsibility of the initializer to respect
324326
idempotency!
325327

328+
You can check to see that the repository has been initialized by dumping the
329+
content repository:
330+
331+
.. code-block:: bash
332+
333+
$ php app/console doctrine:phpcr:node:dump
334+
326335
Create Data Fixtures
327336
~~~~~~~~~~~~~~~~~~~~
328337

338+
You can use the doctrine data fixtures library to define some initial data for
339+
your CMS.
340+
341+
Ensure that you have the following package installed:
342+
343+
.. code-block:: javascript
344+
345+
{
346+
...
347+
require: {
348+
...
349+
"doctrine/data-fixtures": "1.0.0"
350+
},
351+
...
352+
}
353+
329354
Create a page for your CMS::
330355

331356
// src/Acme/BasicCmsBundle/DataFixtures/PHPCR/LoadPageData.php
@@ -343,7 +368,7 @@ Create a page for your CMS::
343368
$parent = $dm->find(null, '/cms/pages');
344369

345370
$page = new Page();
346-
$page->setTitle
371+
$page->setTitle('Home');
347372
$page->setParent($parent);
348373
$page->setContent(<<<HERE
349374
Welcome to the homepage of this really basic CMS.

cookbook/creating_a_cms/020-auto-routing.rst

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Part 2: Automatic Routing
2-
-------------------------
1+
Routing and Automatic Routing
2+
-----------------------------
33

44
The routes (URLs) to your content will be automatically created and updated
55
using the RoutingAutoBundle. This bundle uses a configuration language to
@@ -23,6 +23,47 @@ the contents will be avilable at the following URLs:
2323
* **About**: ``http://localhost:8000/page/about``
2424
* etc.
2525

26+
Installation
27+
~~~~~~~~~~~~
28+
29+
Ensure that you have the following package installed:
30+
31+
.. code-block:: javascript
32+
33+
{
34+
...
35+
require: {
36+
...
37+
"symfony-cmf/routing-auto-bundle": "1.0.*@alpha"
38+
},
39+
...
40+
}
41+
42+
.. note::
43+
44+
You are installing the bleeding edge version of the routing-auto bundle.
45+
46+
Enable the routing bundles to your kernel::
47+
48+
class AppKernel extends Kernel
49+
{
50+
public function registerBundles()
51+
{
52+
$bundles = array(
53+
// ...
54+
new Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(),
55+
new Symfony\Cmf\Bundle\RoutingAutoBundle\CmfRoutingAutoBundle(),
56+
);
57+
58+
// ...
59+
}
60+
}
61+
62+
.. note::
63+
64+
The `symfony-cmf/routing-bundle` package is installed automatically as
65+
`symfony-cmf/routing-auto-bundle` depends on it.
66+
2667
Enable the Dynamic Router
2768
~~~~~~~~~~~~~~~~~~~~~~~~~
2869

cookbook/creating_a_cms/030-sonata-admin.rst

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
Part 3 - The Backend
2-
--------------------
1+
The Backend - Sonata Admin
2+
--------------------------
33

44
In this chapter you will build an administration interface with the help
55
of the `SonataAdminBundle`_.
66

7-
Configure Sonata
8-
~~~~~~~~~~~~~~~~
7+
Installation
8+
~~~~~~~~~~~~
9+
10+
Install the following package is installed:
11+
12+
.. code-block:: javascript
13+
14+
{
15+
...
16+
require: {
17+
...
18+
"sonata-project/doctrine-phpcr-admin-bundle": "dev-master",
19+
},
20+
...
21+
}
922
1023
Enable the Sonata related bundles to your kernel::
1124

@@ -21,6 +34,7 @@ Enable the Sonata related bundles to your kernel::
2134
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
2235
new Sonata\DoctrinePHPCRAdminBundle\SonataDoctrinePHPCRAdminBundle(),
2336
new Sonata\AdminBundle\SonataAdminBundle(),
37+
new Sonata\CoreBundle\SonataCoreBundle(),
2438
);
2539

2640
// ...
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
Controllers and Templates
2+
-------------------------
3+
4+
Go to the URL http://localhost:8000/page/home in your browser - this should be
5+
your page, but it says that it cannot find a controller. In other words it has
6+
found the *page referencing route* for your page but Symfony does not know what
7+
to do with it.
8+
9+
You can map a default controller for all instances of ``Page``:
10+
11+
.. configuration-block::
12+
13+
.. code-block:: yaml
14+
15+
# app/config/config.yml
16+
cmf_routing:
17+
dynamic:
18+
# ...
19+
controllers_by_class:
20+
Acme\BasicCmsBundle\Document\Page: Acme\BasicCmsBundle\Controller\DefaultController::pageAction
21+
22+
.. code-block:: xml
23+
24+
<!-- app/config/config.xml -->
25+
<?xml version="1.0" encoding="UTF-8" ?>
26+
27+
<container xmlns="http://cmf.symfony.com/schema/dic/services"
28+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
29+
30+
<config xmlns="http://cmf.symfony.com/schema/dic/routing">
31+
<dynamic generic-controller="cmf_content.controller:indexAction">
32+
<!-- ... -->
33+
<controllers-by-class
34+
class="Acme\BasicCmsBundle\Document\Page"
35+
>
36+
Acme\BasicCmsBundle\Controller\DefaultController::pageAction
37+
</controllers-by-class>
38+
</dynamic>
39+
</config>
40+
</container>
41+
42+
.. code-block:: php
43+
44+
// app/config/config.php
45+
$container->loadFromExtension('cmf_routing', array(
46+
'dynamic' => array(
47+
// ...
48+
'controllers_by_class' => array(
49+
'Acme\BasicCmsBundle\Document\Page' => 'Acme\BasicCmsBundle\Controller\DefaultController::pageAction',
50+
),
51+
),
52+
));
53+
54+
This will cause requests to be forwarded to this controller when the route
55+
which matches the incoming request is provided by the dynamic router **and**
56+
the content document that that route references is of class
57+
``Acme\BasicCmsBundle\Document\Page``.
58+
59+
Now create the action in the default controller - you can pass the ``Page``
60+
object and all the ``Posts`` to the view::
61+
62+
// src/Acme/BasicCmsBundle/Controller/DefaultController.php
63+
64+
// ...
65+
class DefaultController extends Controller
66+
{
67+
// ...
68+
69+
/**
70+
* @Template()
71+
*/
72+
public function pageAction($contentDocument)
73+
{
74+
$dm = $this->get('doctrine_phpcr')->getManager();
75+
$posts = $dm->getRepository('Acme\BasicCmsBundle\Document\Post')->findAll();
76+
77+
return array(
78+
'page' => $contentDocument,
79+
'posts' => $posts,
80+
);
81+
}
82+
}
83+
84+
The ``Page`` object is passed automatically as ``$contentDocument``.
85+
86+
Add a corresponding twig template (note that this works because you use the
87+
``@Template`` annotation):
88+
89+
.. configuration-block::
90+
91+
.. code-block:: html+jinja
92+
93+
{# src/Acme/BasicCmsBundle/Resources/views/Default/page.html.twig #}
94+
<h1>{{ page.title }}</h1>
95+
<p>{{ page.content|raw }}</p>
96+
<h2>Our Blog Posts</h2>
97+
<ul>
98+
{% for post in posts %}
99+
<li><a href="{{ path(post) }}">{{ post.title }}</a></li>
100+
{% endfor %}
101+
</ul>
102+
103+
.. code-block:: html+php
104+
105+
<!-- src/Acme/BasicCmsBundle/Resources/views/Default/page.html.twig -->
106+
<h1><?php echo $page->getTitle() ?></h1>
107+
<p><?php echo $page->getContent() ?></p>
108+
<h2>Our Blog Posts</h2>
109+
<ul>
110+
<?php foreach($posts as $post) : ?>
111+
<li>
112+
<a href="<?php echo $view['router']->generate($post) ?>">
113+
<?php echo $post->getTitle() ?>
114+
</a>
115+
</li>
116+
<?php endforeach ?>
117+
</ul>
118+
119+
Now have another look at: http://localhost:8000/page/home
120+
121+
Notice what is happening with the post object and the ``path`` function - you
122+
pass the ``Post`` object and the ``path`` function will pass the object to the
123+
router and because it implements the ``RouteReferrersReadInterface`` the
124+
``DynamicRouter`` will be able to generate the URL for the post.
125+
126+
Click on a ``Post`` and you will have the same error that you had before when
127+
viewing the page at ``/home`` and you can resolve it in the same way.

0 commit comments

Comments
 (0)