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

Commit d8d7add

Browse files
committed
make the route code a working example. fix #404
1 parent 99e7049 commit d8d7add

File tree

2 files changed

+102
-60
lines changed

2 files changed

+102
-60
lines changed

book/routing.rst

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -440,25 +440,46 @@ As mentioned above, you can use any route provider. The example in this
440440
section applies if you use the default PHPCR-ODM route provider
441441
(``Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RouteProvider``).
442442

443-
All routes are located under a configured root path, for example
444-
``/cms/routes``. A new route can be created in PHP code as follows::
443+
All routes are located under the base path configured in the application
444+
configuration ``cmf_routing.persistence.phpcr.route_basepath``. By default
445+
this path is ``/cms/routes``. A new route can be created in PHP code as
446+
follows::
445447

446-
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
447-
use Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\Content;
448-
449-
$route = new Route();
450-
$route->setParent($dm->find(null, '/routes'));
451-
$route->setName('projects');
448+
// src/Acme/MainBundle/DataFixtures/PHPCR/LoadRoutingData.php
449+
namespace Acme\DemoBundle\DataFixtures\PHPCR;
452450

453-
// link a content to the route
454-
$content = new Content('my content');
455-
$route->setRouteContent($content);
456-
457-
// now define an id parameter; do not forget the leading slash if you
458-
// want /projects/{id} and not /projects{id}
459-
$route->setVariablePattern('/{id}');
460-
$route->setRequirement('id', '\d+');
461-
$route->setDefault('id', 1);
451+
use Doctrine\ODM\PHPCR\DocumentManager;
452+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
453+
use Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent;
454+
455+
class LoadRoutingData implements FixtureInterface
456+
{
457+
/**
458+
* @param DocumentManager $dm
459+
*/
460+
public function load(ObjectManager $dm)
461+
{
462+
$route = new Route();
463+
$route->setParent($dm->find(null, '/cms/routes'));
464+
$route->setName('projects');
465+
466+
// link a content to the route
467+
$content = new StaticContent();
468+
$content->setParent($dm->find(null, '/cms/content'));
469+
$content->setName('my-content');
470+
$dm->persist($content);
471+
$route->setRouteContent($content);
472+
473+
// now define an id parameter; do not forget the leading slash if you
474+
// want /projects/{id} and not /projects{id}
475+
$route->setVariablePattern('/{id}');
476+
$route->setRequirement('id', '\d+');
477+
$route->setDefault('id', 1);
478+
479+
$dm->persist($route),
480+
$dm->flush();
481+
}
482+
}
462483

463484
This will give you a document that matches the URL ``/projects/<number>`` but
464485
also ``/projects`` as there is a default for the id parameter.

book/simplecms.rst

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,79 +41,100 @@ Creating a new Page
4141
~~~~~~~~~~~~~~~~~~~
4242

4343
To create a page, use the
44-
``Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page`` object (which
45-
extends from ``Symfony\Cmf\Bundle\SimpleCmsBundle\Model\Page``)::
44+
``Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page`` object::
4645

47-
use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;
48-
49-
$page = new Page();
50-
$page->setTitle('About Symfony CMF');
51-
$page->setLabel('About');
52-
$page->setBody(...);
46+
// // src/Acme/MainBundle/DataFixtures/PHPCR/LoadSimpleCms.php
47+
namespace Acme\DemoBundle\DataFixtures\PHPCR;
5348

54-
You can also set other things (e.g. tags).
49+
use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;
50+
use Doctrine\ODM\PHPCR\DocumentManager;
51+
52+
class LoadRoutingData implements FixtureInterface
53+
{
54+
/**
55+
* @param DocumentManager $dm
56+
*/
57+
public function load(ObjectManager $dm)
58+
{
59+
$parent = $dm->find(null, '/cms/simple');
60+
$page = new Page();
61+
$page->setTitle('About Symfony CMF');
62+
$page->setLabel('About');
63+
$page->setBody(...);
64+
65+
// the tree position defines the URL
66+
$page->setPosition($parent, 'about');
67+
68+
$dm->persist($page);
69+
$dm->flush();
70+
}
71+
}
72+
73+
You can also set other options on the Page (e.g. tags).
5574

5675
All pages are stored in a simple tree structure. To set the position, use
57-
``setPosition``. The first argument is the name for current page and the
58-
second argument is the parent node. The name of the first argument is used as
59-
the route. For instance, if you have this tree structure:
76+
``setPosition``. The first argument is the parent document, the second the
77+
name for this page. The names are used for the URL. For instance, you may
78+
have the following tree structure:
6079

6180
.. code-block:: text
6281
6382
/cmf/simple/
64-
home/
6583
about/
6684
blog/
6785
symfony-cmf-is-great/
6886
69-
In this case, you have 4 pages: ``home``, ``about``, ``blog`` and
70-
``symfony-cmf-is-great``. The page ``symfony-cmf-is-great`` is a child of
71-
``blog`` and thus has the url ``/blog/symfony-cmf-is-great``. To create such a
72-
structure, the code looks like this::
87+
In this case, you have 4 pages: the page at ``/cms/simple``, ``about``,
88+
``blog`` and ``symfony-cmf-is-great``. The page at the home has the path
89+
``/``. The page ``symfony-cmf-is-great`` is a child of ``blog`` and thus
90+
has the path ``/blog/symfony-cmf-is-great``. To create such a
91+
structure, you would do::
7392

74-
use PHPCR\Util\NodeHelper;
75-
use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;
76-
77-
// the PHPCR-ODM manager, more on that later
78-
$manager = ...;
7993

80-
$root = NodeHelper::createPath($manager->getPhpcrSession(), '/cmf/simple');
94+
// // src/Acme/MainBundle/DataFixtures/PHPCR/LoadSimpleCms.php
95+
namespace Acme\DemoBundle\DataFixtures\PHPCR;
8196

82-
$home = new Page();
83-
// ... set up home
84-
$home->setPosition($root, 'home');
97+
use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;
98+
use Doctrine\ODM\PHPCR\DocumentManager;
8599

86-
$manager->persist($home); // add home to the database
100+
class LoadRoutingData implements FixtureInterface
101+
{
102+
/**
103+
* @param DocumentManager $dm
104+
*/
105+
public function load(ObjectManager $dm)
106+
{
107+
$root = $dm->find(null, '/cms/simple');
87108

88-
$about = new Page();
89-
// ... set up about
90-
$about->setPosition($root, 'about');
109+
$about = new Page();
110+
// ... set up about
111+
$about->setPosition($root, 'about');
91112

92-
$manager->persist($about); // add about to the database
113+
$dm->persist($about);
93114

94-
$blog = new Page();
95-
// ... set up blog
96-
$blog->setPosition($root, 'blog');
115+
$blog = new Page();
116+
// ... set up blog
117+
$blog->setPosition($root, 'blog');
97118

98-
$manager->persist($blog); // add blog to the database
119+
$dm->persist($blog);
99120

100-
$blogPost = new Page();
101-
// ... set up blog post
102-
$blogPost->setPosition($blog, 'symfony-cmf-is-great');
121+
$blogPost = new Page();
122+
// ... set up blog post
123+
$blogPost->setPosition($blog, 'symfony-cmf-is-great');
103124

104-
$manager->persist($blogPost); // add blog post to the database
125+
$dm->persist($blogPost);
105126

106-
// as with all doctrine variants, the changes are only saved when the
107-
// flush method is called
108-
$manager->flush();
127+
$dm->flush();
128+
}
129+
}
109130

110131
Every PHPCR-ODM document must have a parent document. Parents are never
111132
created automatically, so we use the PHPCR NodeHelper to ensure we have
112133
the root element (``/cmf/simple`` in this case).
113134

114135
.. note::
115136

116-
The ``/cmf/simple`` basepath is actually already created by an
137+
The Page at ``/cmf/simple`` is created by an
117138
:ref:`initializer <phpcr-odm-repository-initializers>` of the
118139
SimpleCmsBundle.
119140

0 commit comments

Comments
 (0)