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

Commit f4b71b9

Browse files
committed
wrap up rewriting the quick tour
1 parent 8e49370 commit f4b71b9

File tree

7 files changed

+117
-244
lines changed

7 files changed

+117
-244
lines changed
Loading

quick_tour/the_big_picture.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ The Request Flow
9494
Now, the Sandbox is ready to use. Navigate to the homepage
9595
(``http://localhost:8000/``) to see the demo:
9696

97-
redo image with current sandbox
98-
9997
.. image:: ../_images/quick_tour/big-picture-home.png
10098

10199
You see that we already have a complete website in our demo. Let's take a

quick_tour/the_model.rst

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ dump command:
7777
.. note::
7878

7979
Previously, the PHPCR tree was compared with a Filesystem. While this
80-
gives you a good image of what happens, it's not the truth. You can
81-
better compare it to an XML file, where each node is an element and its
80+
gives you a good image of what happens, it's not the only truth. You can
81+
compare it to an XML file, where each node is an element and its
8282
properties are attributes.
8383

8484
Doctrine PHPCR-ODM
@@ -90,13 +90,13 @@ directly persisted into and retrieved from the PHPCR tree. This is similar to
9090
the Doctrine ORM provided by default in the Symfony Standard Edition, but for
9191
PHPCR instead of SQL databases.
9292

93-
Creating a Page with code
94-
-------------------------
93+
Creating Content from Code
94+
--------------------------
9595

96-
Now you know a little bit more about PHPCR and you know the tool to interact
96+
Now that you know a little bit more about PHPCR and you know the tool to interact
9797
with it, you can start using it yourself. In the previous chapter, you edited
9898
a page by using a yaml file which was parsed by the fixture loader of the
99-
sandbox. This time, you'll create a page by doing it yourself.
99+
sandbox. This time, you'll create a page with PHP code.
100100

101101
First, you have to create a new DataFixture to add your new page. You do this
102102
by creating a new class in the AppBundle::
@@ -108,7 +108,7 @@ by creating a new class in the AppBundle::
108108
use Doctrine\Common\DataFixtures\FixtureInterface;
109109
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
110110

111-
class LoadPageData implements FixtureInterface, OrderedFixtureInterface
111+
class LoadQuickTourData implements FixtureInterface, OrderedFixtureInterface
112112
{
113113
public function getOrder()
114114
{
@@ -126,84 +126,46 @@ The ``$documentManager`` is the object which will persist the document to
126126
PHPCR. But first, you have to create a new Page document::
127127

128128
use Doctrine\ODM\PHPCR\DocumentManager;
129-
use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;
129+
use Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent;
130130

131131
// ...
132132
public function load(ObjectManager $documentManager)
133133
{
134134
if (!$documentManager instanceof DocumentManager) {
135-
$class = get_class($documentManager);
136-
throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '$class' given.");
135+
throw new \RuntimeException(sprintf(
136+
'Fixture requires a PHPCR ODM DocumentManager instance, instance of "%s" given.',
137+
get_class($documentManager)
138+
));
137139
}
138140

139-
$page = new Page(); // create a new Page object (document)
140-
$page->setName('quick-tour'); // the name of the node
141+
$content = new StaticContent();
142+
$content->setName('quick-tour'); // the name of the node
143+
$content->setTitle('Quick tour new Page');
144+
$content->setBody('I have added this page myself!');
141145

142-
vno sandbox übernehmen
143-
144-
$page->setLabel('Another new Page');
145-
$page->setTitle('Another new Page');
146-
$page->setBody('I have added this page myself!');
147-
}
148-
149-
Each document needs a parent. In this case, the parent should just be the root
146+
Each document needs a parent. In this case, the parent should be the content root
150147
node. To do this, we first retrieve the root document from PHPCR and then set
151148
it as its parent::
152149

153150
public function load(ObjectManager $documentManager)
154151
{
155152
// ...
156-
// get root document (/cms/simple)
157-
$simpleCmsRoot = $documentManager->find(null, '/cms/simple');
158-
159-
$page->setParentDocument($simpleCmsRoot); // set the parent to the root
160-
}
153+
// get root document
154+
$contentRoot = $documentManager->find(null, '/cms/content');
155+
$content->setParentDocument($contentRoot); // set the parent to the root
161156

162-
And at last, we have to tell the Document Manager to persist our Page
157+
And finally, we have to tell the Document Manager to persist our content
163158
document using the Doctrine API::
164159

165160
public function load(ObjectManager $documentManager)
166161
{
167162
// ...
168-
$documentManager->persist($page); // add the Page in the queue
169-
$documentManager->flush(); // add the Page to PHPCR
163+
$documentManager->persist($content); // tell the document manager to track the content
164+
$documentManager->flush(); // doctrine is like a toilet: never forget to flush
170165
}
171166

172167
Now you need to execute the ``doctrine:phpcr:fixtures:load`` command again.
173-
When dumping the nodes again, your new page should turn up under /cms/content.
174-
175-
To actually see this page in the browser, we need a route::
176-
177-
public function load(ObjectManager $documentManager)
178-
{
179-
// ...
180-
$route = new Route();
181-
$routeRoot = $documentManager->find(null, '/cms/routes/en');
182-
$route->setPosition($routeRoot, 'quick-tour');
183-
$route->setContent($page);
184-
$documentManager->persist($route);
185-
}
186-
187-
And we add a menu entry to link to this page, and flush the document manager
188-
at the end::
189-
190-
public function load(ObjectManager $documentManager)
191-
{
192-
$menu = new MenuNode();
193-
$menu->setName('new_page');
194-
$menu->setLabel('Quick Tour');
195-
$menu->setContent($page);
196-
$menuMain = $documentManager->find(null, '/cms/menu/main');
197-
$menu->setParentDocument($menuMain);
198-
$documentManager->persist($menu);
199-
200-
$documentManager->flush();
201-
}
202-
203-
Re-run the fixtures loading command and then refresh the web site. Your new
204-
page will be added, with a menu entry at the bottom of the menu!
205-
206-
.. image:: ../_images/quick_tour/the-model-new-page.png
168+
When dumping the nodes again, your new page should turn up under ``/cms/content/quick-tour``.
207169

208170
.. seealso::
209171

quick_tour/the_router.rst

Lines changed: 30 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
The Router
55
==========
66

7-
Welcome at the third part of the Quick Tour. You seem to have fallen in love
8-
with the CMF, getting this far! And that's a good thing, as you will learn
9-
about the backbone of the CMF in this chapter: The Router.
7+
Welcome at the third part of the Quick Tour. Well done, making it this far!
8+
And that's a good thing, as you will learn about the backbone of the CMF in
9+
this chapter: The Router.
1010

1111
The Backbone of the CMF
1212
-----------------------
1313

14-
As already said, the router is central to the CMF. To understand this, let us
14+
The router is central to the CMF. To understand this, let us
1515
look at what a CMS tries to do. In a normal Symfony application, a route
1616
refers to a controller which can handle a specific entity. Another route
1717
refers to another controller which can handle another entity. This way, a
1818
route is tied to a controller. In fact, using the Symfony core you are also
19-
limited at this.
19+
limited by this pattern.
2020

21-
But if you look at the base of a CMS, it only needs to handle 1 type of
21+
But if you look at the base of a CMS, it only needs to handle one type of
2222
entity: The Content. So most of the routes don't have to be tied to a
2323
controller anymore, as only one controller is needed. The Route has to be tied
24-
to a specific Content object, which - on its side - can reference a specific
24+
to a specific Content object, which - on its side - may need a specific
2525
template and controller.
2626

27-
Other parts of the CMF are also related to the Router. To give 2 examples: The
27+
Other parts of the CMF are also related to the Router. Two examples: The
2828
menu is created by generating specific routes using the Router and the blocks
2929
are displayed to specific routes (as they are related to a template).
3030

@@ -35,10 +35,8 @@ In the first chapter, you have already learned that routes are loaded from the
3535
database using a special ``DynamicRouter``. This way, not all routes need to
3636
be loaded each request.
3737

38-
Matching routes from a PHPCR is really simple. If you remember the previous
39-
chapter, you know that you can get the ``quick_tour`` page from PHPCR using
40-
``/cms/routes/en/quick-tour``. The URL to get this page is ``en/quick-tour``. Some
41-
other examples:
38+
Matching routes from a PHPCR is straightforward: The router takes the request
39+
path and looks for a document with that path. Some examples:
4240

4341
.. code-block:: text
4442
@@ -58,164 +56,62 @@ all routes are prefixed with ``/cms/routes``.
5856
You see that a route like ``/company/team``, which consist of 2 "path units",
5957
has 2 documents in the PHPCR tree: ``company`` and ``team``.
6058

61-
Chaining multiple Routers
62-
-------------------------
63-
64-
You may need to have several prefixes or several routes. For instance, you may
65-
want to use both the ``DynamicRouter`` for the page routes, but also the
66-
static routing files from Symfony for your custom logic. To be able to do that,
67-
the CMF provides a ``ChainRouter``. This router chains over multiple router
68-
and stops whenever a router matches.
69-
70-
By default, the ``ChainRouter`` overrides the Symfony router and only has the
71-
core router in its chain. You can add more routers to the chain in the
72-
configuration or by tagging the router services with ``cmf_routing.router``.
73-
7459
Creating a new Route
7560
--------------------
7661

77-
Now you know the basics of routing, you can add a new route to the tree. In
78-
the configuration file, configure a new chain router so that you can put your
79-
new routes in ``/cms/routes``:
80-
81-
.. configuration-block::
82-
83-
.. code-block:: yaml
84-
85-
# app/config/config.yml
86-
87-
# ...
88-
cmf_routing:
89-
chain:
90-
routers_by_id:
91-
# the standard DynamicRouter
92-
cmf_routing.dynamic_router: 200
93-
94-
# the core symfony router
95-
router.default: 100
96-
dynamic:
97-
persistence:
98-
phpcr:
99-
route_basepaths:
100-
- /cms/routes
101-
# /cms/routes is the default base path, the above code is
102-
# equivalent to:
103-
# phpcr: true
104-
105-
.. code-block:: xml
106-
107-
<!-- app/config/config.xml -->
108-
<?xml version="1.0" encoding="UTF-8" ?>
109-
<container xmlns="http://symfony.com/schema/dic/services">
110-
<!-- ... -->
111-
112-
<config xmlns="http://cmf.symfony.com/schema/dic/routing">
113-
<chain>
114-
<!-- the standard DynamicRouter -->
115-
<router-by-id id="cmf_routing.dynamic_router">200</router-by-id>
116-
117-
<!-- the core symfony router -->
118-
<router-by-id id="router.default">100</router-by-id>
119-
</chain>
120-
121-
<dynamic>
122-
<persistence>
123-
<phpcr>
124-
<route-basepath>/cms/routes</route-basepath>
125-
</phpcr>
126-
<!-- /cms/routes is the default base path, the above
127-
code is equivalent to:
128-
<phpcr />
129-
--->
130-
</persistence>
131-
</dynamic>
132-
</config>
133-
</container>
134-
135-
.. code-block:: php
136-
137-
// app/config/config.php
138-
$container->loadFromExtension('cmf_routing', [
139-
'chain' => [
140-
'routers_by_id' => [
141-
// the standard DynamicRouter
142-
'cmf_routing.dynamic_router' => 200,
143-
144-
// the core symfony router
145-
'router.default' => 100,
146-
],
147-
],
148-
'dynamic' => [
149-
'persistence' => [
150-
'phpcr' => [
151-
'route_basepaths' => '/cms/routes',
152-
],
153-
/* /cms/routes is the default base path, the above code is
154-
equivalent to:
155-
'phpcr' => true,
156-
*/
157-
],
158-
],
159-
]);
160-
161-
Now you can add a new ``Route`` to the tree using Doctrine::
162-
163-
// src/AppBundle/DataFixtures/PHPCR/LoadExtraRoutingData.php
62+
Now you know the basics of routing, you can add a new route to the tree using
63+
Doctrine::
64+
65+
// src/AppBundle/DataFixtures/PHPCR/LoadQuickTourData.php
16466
namespace AppBundle\DataFixtures\PHPCR;
16567

16668
use Doctrine\Common\Persistence\ObjectManager;
16769
use Doctrine\Common\DataFixtures\FixtureInterface;
16870
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
16971
use Doctrine\ODM\PHPCR\DocumentManager;
170-
17172
use PHPCR\Util\NodeHelper;
172-
17373
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
17474

175-
class LoadExtraRoutingData implements FixtureInterface, OrderedFixtureInterface
75+
class LoadQuickTourData implements FixtureInterface, OrderedFixtureInterface
17676
{
177-
public function getOrder()
178-
{
179-
return 21;
180-
}
181-
18277
public function load(ObjectManager $documentManager)
18378
{
184-
if (!$documentManager instanceof DocumentManager) {
185-
$class = get_class($documentManager);
186-
throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '$class' given.");
187-
}
79+
// static content from model chapter, resulting in $content being defined
80+
// ...
18881

18982
$routesRoot = $documentManager->find(null, '/cms/routes');
190-
19183
$route = new Route();
19284
// set $routesRoot as the parent and 'new-route' as the node name,
19385
// this is equal to:
19486
// $route->setName('new-route');
19587
// $route->setParentDocument($routesRoot);
19688
$route->setPosition($routesRoot, 'new-route');
19789

198-
$page = $documentManager->find(null, '/cms/content/quick');
199-
$route->setContent($page);
90+
$route->setContent($content);
20091

20192
$documentManager->persist($route); // put $route in the queue
20293
$documentManager->flush(); // save it
20394
}
20495
}
20596

206-
Above we implemented the ``OrderedFixtureInterface`` so that our routes were loaded in the correct sequence relative to other fixtures.
207-
20897
Now execute the ``doctrine:phpcr:fixtures:load`` command again.
20998

21099
This creates a new node called ``/cms/routes/new-route``, which will display
211100
our ``quick_tour`` page when you go to ``/new-route``.
212101

213-
.. tip::
102+
.. image:: ../_images/quick_tour/the-router-new-page.png
214103

215-
When doing this in a real app, you may want to use a ``RedirectRoute``
216-
instead.
104+
Chaining multiple Routers
105+
-------------------------
106+
107+
Usually, you want to use both the ``DynamicRouter`` for the editable routes,
108+
but also the static routing files from Symfony for your custom logic. To be
109+
able to do that, the CMF provides a ``ChainRouter``. This router tries each
110+
registered router and stops on the first router that returns a match.
217111

218-
.. TODO write something about templates_by_class, etc.
112+
By default, the ``ChainRouter`` overrides the Symfony router and only has the
113+
core and dynamic router in its chain. You can add more routers to the chain in the
114+
configuration or by tagging the router services with ``cmf_routing.router``.
219115

220116
Final Thoughts
221117
--------------
@@ -225,7 +121,7 @@ basics of the Symfony CMF. First, you have learned about the Request flow and
225121
quickly learned each new step in this process. After that, you have learned
226122
more about the default storage layer and the routing system.
227123

228-
The Routing system is created together with some developers from Drupal8. In
124+
The Routing system is created together with some developers from Drupal 8. In
229125
fact, Drupal 8 uses the Routing component of the Symfony CMF. The Symfony CMF
230126
also uses some 3rd party bundles from others and integrated them into PHPCR.
231127
In :doc:`the next chapter <the_third_party_bundles>` you'll learn more about

0 commit comments

Comments
 (0)