4
4
The Router
5
5
==========
6
6
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.
10
10
11
11
The Backbone of the CMF
12
12
-----------------------
13
13
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
15
15
look at what a CMS tries to do. In a normal Symfony application, a route
16
16
refers to a controller which can handle a specific entity. Another route
17
17
refers to another controller which can handle another entity. This way, a
18
18
route is tied to a controller. In fact, using the Symfony core you are also
19
- limited at this.
19
+ limited by this pattern .
20
20
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
22
22
entity: The Content. So most of the routes don't have to be tied to a
23
23
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
25
25
template and controller.
26
26
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
28
28
menu is created by generating specific routes using the Router and the blocks
29
29
are displayed to specific routes (as they are related to a template).
30
30
@@ -35,10 +35,8 @@ In the first chapter, you have already learned that routes are loaded from the
35
35
database using a special ``DynamicRouter ``. This way, not all routes need to
36
36
be loaded each request.
37
37
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:
42
40
43
41
.. code-block :: text
44
42
@@ -58,164 +56,62 @@ all routes are prefixed with ``/cms/routes``.
58
56
You see that a route like ``/company/team ``, which consist of 2 "path units",
59
57
has 2 documents in the PHPCR tree: ``company `` and ``team ``.
60
58
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
-
74
59
Creating a new Route
75
60
--------------------
76
61
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
164
66
namespace AppBundle\DataFixtures\PHPCR;
165
67
166
68
use Doctrine\Common\Persistence\ObjectManager;
167
69
use Doctrine\Common\DataFixtures\FixtureInterface;
168
70
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
169
71
use Doctrine\ODM\PHPCR\DocumentManager;
170
-
171
72
use PHPCR\Util\NodeHelper;
172
-
173
73
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
174
74
175
- class LoadExtraRoutingData implements FixtureInterface, OrderedFixtureInterface
75
+ class LoadQuickTourData implements FixtureInterface, OrderedFixtureInterface
176
76
{
177
- public function getOrder()
178
- {
179
- return 21;
180
- }
181
-
182
77
public function load(ObjectManager $documentManager)
183
78
{
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
+ // ...
188
81
189
82
$routesRoot = $documentManager->find(null, '/cms/routes');
190
-
191
83
$route = new Route();
192
84
// set $routesRoot as the parent and 'new-route' as the node name,
193
85
// this is equal to:
194
86
// $route->setName('new-route');
195
87
// $route->setParentDocument($routesRoot);
196
88
$route->setPosition($routesRoot, 'new-route');
197
89
198
- $page = $documentManager->find(null, '/cms/content/quick');
199
- $route->setContent($page);
90
+ $route->setContent($content);
200
91
201
92
$documentManager->persist($route); // put $route in the queue
202
93
$documentManager->flush(); // save it
203
94
}
204
95
}
205
96
206
- Above we implemented the ``OrderedFixtureInterface `` so that our routes were loaded in the correct sequence relative to other fixtures.
207
-
208
97
Now execute the ``doctrine:phpcr:fixtures:load `` command again.
209
98
210
99
This creates a new node called ``/cms/routes/new-route ``, which will display
211
100
our ``quick_tour `` page when you go to ``/new-route ``.
212
101
213
- .. tip ::
102
+ .. image :: ../_images/quick_tour/the-router-new-page.png
214
103
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.
217
111
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 ``.
219
115
220
116
Final Thoughts
221
117
--------------
@@ -225,7 +121,7 @@ basics of the Symfony CMF. First, you have learned about the Request flow and
225
121
quickly learned each new step in this process. After that, you have learned
226
122
more about the default storage layer and the routing system.
227
123
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
229
125
fact, Drupal 8 uses the Routing component of the Symfony CMF. The Symfony CMF
230
126
also uses some 3rd party bundles from others and integrated them into PHPCR.
231
127
In :doc: `the next chapter <the_third_party_bundles >` you'll learn more about
0 commit comments