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

Commit 9333185

Browse files
dbuacrobat
andauthored
document new way of generating routes (#864)
* document new way of generating routes Co-Authored-By: Jeroen Thora <[email protected]>
1 parent 26f259b commit 9333185

File tree

5 files changed

+68
-36
lines changed

5 files changed

+68
-36
lines changed

bundles/core/templating.rst

+22-6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ Helper methods
112112
Code examples
113113
.............
114114

115+
.. versionadded:: 2.3
116+
117+
Since `symfony-cmf/routing: 2.3.0`, the route document should be passed in
118+
the route parameters as `_route_object`, and the special route name
119+
`cmf_routing_object` is to be used. When using older versions of routing,
120+
you need to pass the route document as route name.
121+
115122
.. configuration-block::
116123

117124
.. code-block:: html+jinja
@@ -121,16 +128,16 @@ Code examples
121128
{% if cmf_is_published(page) %}
122129
{% set prev = cmf_prev_linkable(page) %}
123130
{% if prev %}
124-
<a href="{{ path(prev) }}">prev</a>
131+
<a href="{{ path('cmf_routing_object', {_route_object: prev}) }}">prev</a>
125132
{% endif %}
126133

127134
{% set next = cmf_next_linkable(page) %}
128135
{% if next %}
129-
<span style="float: right; padding-right: 40px;"><a href="{{ path(next) }}">next</a></span>
136+
<span style="float: right; padding-right: 40px;"><a href="{{ path('cmf_routing_object', {_route_object: next}) }}">next</a></span>
130137
{% endif %}
131138

132139
{% for news in cmf_children(parent=cmfMainContent, class='AppBundle\\Document\\NewsItem')|reverse %}
133-
<li><a href="{{ path(news) }}">{{ news.title }}</a> ({{ news.publishStartDate | date('Y-m-d') }})</li>
140+
<li><a href="{{ path('cmf_routing_object', {_route_object: news}) }}">{{ news.title }}</a> ({{ news.publishStartDate | date('Y-m-d') }})</li>
134141
{% endfor %}
135142

136143
{% if 'de' in cmf_document_locales(page) %}
@@ -158,19 +165,28 @@ Code examples
158165
<?php if $view['cmf']->isPublished($page) : ?>
159166
<?php $prev = $view['cmf']->getPrev($page) ?>
160167
<?php if ($prev) : ?>
161-
<a href="<?php echo $view['router']->generate($prev) ?>">prev</a>
168+
<a href="<?php echo $view['router']->generate(
169+
RouteObjectInterface::OBJECT_BASED_ROUTE_NAME,
170+
[RouteObjectInterface::ROUTE_OBJECT => $prev]
171+
) ?>">prev</a>
162172
<?php endif ?>
163173

164174
<?php $next = $view['cmf']->getNext($page) ?>
165175
<?php if ($next) : ?>
166176
<span style="float: right; padding-right: 40px;">
167-
<a href="<?php echo $view['router']->generate($next) ?>">next</a>
177+
<a href="<?php echo $view['router']->generate(
178+
RouteObjectInterface::OBJECT_BASED_ROUTE_NAME,
179+
[RouteObjectInterface::ROUTE_OBJECT => $next]
180+
) ?>">next</a>
168181
</span>
169182
<?php endif ?>
170183

171184
<?php foreach (array_reverse($view['cmf']->getChildren($page)) as $news) : ?>
172185
<li>
173-
<a href="<?php echo $view['router']->generate($news) ?>"><?php echo $news->getTitle() ?></a>
186+
<a href="<?php echo $view['router']->generate(
187+
RouteObjectInterface::OBJECT_BASED_ROUTE_NAME,
188+
[RouteObjectInterface::ROUTE_OBJECT => $news]
189+
) ?>"><?php echo $news->getTitle() ?></a>
174190
(<?php echo date('Y-m-d', $news->getPublishStartDate()) ?>)
175191
</li>
176192
<?php endforeach ?>

bundles/routing/dynamic.rst

+22-10
Original file line numberDiff line numberDiff line change
@@ -497,24 +497,33 @@ also responsible for generating an URL from a route and its parameters. The
497497
All Twig examples below are given with the ``path`` function that generates
498498
the URL without domain, but will work with the ``url`` function as well.
499499

500-
Also, you can specify parameters to the generator, which will be used if
501-
the route contains a dynamic pattern or otherwise will be appended as
500+
Also, you can specify other parameters to the generator, which will be used
501+
if the route contains a dynamic pattern or otherwise will be appended as
502502
query string, just like with the standard routing.
503503

504-
You can use a ``Route`` object as the name parameter of the generating method.
505-
This will look as follows:
504+
.. versionadded:: 2.3
505+
506+
Since `symfony-cmf/routing: 2.3.0`, the route document should be passed in
507+
the route parameters as `_route_object`, and the special route name
508+
`cmf_routing_object` is to be used. When using older versions of routing,
509+
you need to pass the route document as route name.
510+
511+
You can use a ``Route`` object directly with the router:
506512

507513
.. configuration-block::
508514

509515
.. code-block:: html+jinja
510516

511517
{# myRoute is an object of class Symfony\Component\Routing\Route #}
512-
<a href="{{ path(myRoute) }}">Read on</a>
518+
<a href="{{ path('cmf_routing_object', {_route_object: myRoute}) }}">Read on</a>
513519

514520
.. code-block:: html+php
515521

516522
<!-- $myRoute is an object of class Symfony\Component\Routing\Route -->
517-
<a href="<?php echo $view['router']->generate($myRoute) ?>">
523+
<a href="<?php echo $view['router']->generate(
524+
RouteObjectInterface::OBJECT_BASED_ROUTE_NAME,
525+
[RouteObjectInterface::ROUTE_OBJECT => $myRoute])
526+
?>">
518527
Read on
519528
</a>
520529

@@ -553,12 +562,15 @@ object that implements this interface and provides a route for it:
553562
.. code-block:: html+jinja
554563

555564
{# myContent implements RouteReferrersInterface #}
556-
<a href="{{ path(myContent) }}>Read on</a>
565+
<a href="{{ path('cmf_routing_object', {_route_object: myContent}) }}>Read on</a>
557566

558567
.. code-block:: html+php
559568

560569
<!-- $myContent implements RouteReferrersInterface -->
561-
<a href="<?php echo $view['router']->generate($myContent) ?>">
570+
<a href="<?php echo $view['router']->generate(
571+
RouteObjectInterface::OBJECT_BASED_ROUTE_NAME,
572+
[RouteObjectInterface::ROUTE_OBJECT => $myContent])
573+
?>">
562574
Home
563575
</a>
564576

@@ -575,14 +587,14 @@ an empty route name and tries to find a content implementing the
575587

576588
.. code-block:: html+jinja
577589

578-
<a href="{{ path(null, {'content_id': '/cms/content/my-content'}) }}>
590+
<a href="{{ path('cmf_routing_object', {'content_id': '/cms/content/my-content'}) }}>
579591
Read on
580592
</a>
581593

582594
.. code-block:: html+php
583595

584596
<!-- $myContent implements RouteReferrersInterface -->
585-
<a href="<?php echo $view['router']->generate(null, [
597+
<a href="<?php echo $view['router']->generate('cmf_routing_object', [
586598
'content_id' => '/cms/content/my-content',
587599
]) ?>">
588600
Home

components/routing/dynamic.rst

+16-16
Original file line numberDiff line numberDiff line change
@@ -193,38 +193,38 @@ a route.
193193

194194
The generator method looks like this::
195195

196-
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH);
196+
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH);
197197

198198
In Symfony core, the ``$name`` has to be a string with the configured name
199199
of the route to generate. The CMF routing component adds generators that handle
200200
alternative semantics of ``$name``.
201201

202+
.. versionadded:: 2.3
203+
204+
Since `symfony-cmf/routing: 2.3.0`, the route document should be passed in
205+
the route parameters as `_route_object`, and the special route name
206+
`cmf_routing_object` is to be used. When using older versions of routing,
207+
you need to pass the route document as route name.
208+
202209
The ``ProviderBasedGenerator`` extends Symfony's default
203210
:class:`Symfony\\Component\\Routing\\Generator\\UrlGenerator` (which, in turn,
204211
implements :class:`Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface`)
205-
and - if the name is not already a ``Route`` object - loads the Route from the
206-
Route provider. It then lets the core logic generate the URL from that ``Route``.
212+
and asks the route provider to find a route based on the name and parameters. It
213+
then lets the core logic generate the URL from that ``Route``.
207214

208215
The CMF component also includes the ``ContentAwareGenerator``, which extends
209-
the ``ProviderBasedGenerator``, that checks if ``$name`` is an object
216+
the ``ProviderBasedGenerator``, that checks if ``_route_object`` parameter is an object
210217
implementing ``RouteReferrersReadInterface``. If it is, it gets the ``Route``
211218
from that object. Using the ``ContentAwareGenerator``, you can generate URLs
212219
for your content in three ways:
213220

214-
* Either pass a ``Route`` object as $name
215-
* Or pass a ``RouteReferrersInterface`` object that is your content as $name
221+
* Either pass a ``Route`` object as the ``_route_object`` parameter
222+
* Or pass a ``RouteReferrersInterface`` object that is your content as the ``_route_object`` parameter
216223
* Or provide an implementation of ``ContentRepositoryInterface`` and pass the id
217-
of the content object as parameter ``content_id`` and ``null`` as $name.
218-
219-
If you want to implement your own generator for ``$name`` values that are not
220-
strings, you need to implement the ``ChainedRouterInterface`` and implement the
221-
``supports($name)`` method to tell the ``ChainRouter`` if your router can
222-
accept this ``$name`` to generate a URL.
224+
of the content object as parameter ``content_id`` and ``cmf_routing_object`` as $name.
223225

224-
In order to let the DynamicRouter know if it can try to generate a route with an
225-
object, generators that are able to do so have to implement the
226-
``VersatileGeneratorInterface`` and return true for the ``supports($route)``
227-
call with any object they can handle.
226+
If you want to implement your own generator, implement the ``VersatileGeneratorInterface``
227+
to get better debug messages for when a route can not be generated.
228228

229229
.. _`Event Dispatcher`: https://symfony.com/doc/current/components/event_dispatcher/index.html
230230
.. _`How to create an Event Listener`: https://symfony.com/doc/current/cookbook/event_dispatcher/event_listener.html

tutorial/content-to-controllers.rst

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Make your content route aware
77
In the :doc:`getting-started` section, you defined your `Post` and `Page` documents as
88
implementing the ``RoutesReferrersReadInterface``. This interface enables the routing system
99
to retrieve routes which refer to the object implementing this interface, and this enables
10-
the system to generate a URL (for example when you use ``{{ path(mydocument) }}`` in Twig).
10+
the system to generate a URL (for example when you use
11+
``{{ path('cmf_routing_object', {_route_object: mydocument}) }}`` in Twig).
1112

1213
Earlier you did not have the RoutingBundle installed, so you could not add the mapping.
1314

@@ -141,7 +142,7 @@ Add a corresponding template (note that this works because you use the
141142
<h2>Our Blog Posts</h2>
142143
<ul>
143144
{% for post in posts %}
144-
<li><a href="{{ path(post) }}">{{ post.title }}</a></li>
145+
<li><a href="{{ path('cmf_routing_object', {_route_object: post}) }}">{{ post.title }}</a></li>
145146
{% endfor %}
146147
</ul>
147148

@@ -154,7 +155,10 @@ Add a corresponding template (note that this works because you use the
154155
<ul>
155156
<?php foreach($posts as $post) : ?>
156157
<li>
157-
<a href="<?php echo $view['router']->generate($post) ?>">
158+
<a href="<?php echo $view['router']->generate(
159+
RouteObjectInterface::OBJECT_BASED_ROUTE_NAME,
160+
[RouteObjectInterface::ROUTE_OBJECT => $post]
161+
) ?>">
158162
<?php echo $post->getTitle() ?>
159163
</a>
160164
</li>

tutorial/getting-started.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ be referenceable and in addition will automatically set the date using the
212212
Both the ``Post`` and ``Page`` classes implement the
213213
``RouteReferrersReadInterface``. This interface enables the
214214
:ref:`DynamicRouter to generate URLs <bundles-routing-dynamic-generator>` from
215-
instances of these classes. (for example with ``{{ path(content) }}`` in Twig).
215+
instances of these classes. (for example with ``{{ path('cmf_routing_object', {_route_object: content}) }}`` in Twig).
216216

217217
Repository Initializer
218218
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)