Skip to content

Commit a4ad744

Browse files
committed
Use path() and url() PHP templating helpers
1 parent 3ebf2d0 commit a4ad744

11 files changed

+71
-48
lines changed

Diff for: book/forms.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,9 @@ to the ``form()`` or the ``form_start()`` helper:
10361036

10371037
<!-- app/Resources/views/default/newAction.html.php -->
10381038
<?php echo $view['form']->start($form, array(
1039-
'action' => $view['router']->generate('target_route'),
1039+
// The path() method was introduced in Symfony 2.8. Prior to 2.8,
1040+
// you had to use generate().
1041+
'action' => $view['router']->path('target_route'),
10401042
'method' => 'GET',
10411043
)) ?>
10421044

Diff for: book/from_flat_php_to_symfony2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ database and the Templating component to render a template and return a
598598
<ul>
599599
<?php foreach ($posts as $post): ?>
600600
<li>
601-
<a href="<?php echo $view['router']->generate(
601+
<a href="<?php echo $view['router']->path(
602602
'blog_show',
603603
array('id' => $post->getId())
604604
) ?>">

Diff for: book/http_cache.rst

+6-7
Original file line numberDiff line numberDiff line change
@@ -1087,23 +1087,22 @@ matter), Symfony uses the standard ``render`` helper to configure ESI tags:
10871087

10881088
<!-- app/Resources/views/static/about.html.php -->
10891089

1090-
// you can use a controller reference
1091-
use Symfony\Component\HttpKernel\Controller\ControllerReference;
1090+
<!-- you can use a controller reference -->
10921091
<?php echo $view['actions']->render(
1093-
new ControllerReference(
1092+
new \Symfony\Component\HttpKernel\Controller\ControllerReference(
10941093
'AppBundle:News:latest',
10951094
array('maxPerPage' => 5)
10961095
),
10971096
array('strategy' => 'esi')
10981097
) ?>
10991098

1100-
// ... or a URL
1101-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1099+
<!-- ... or a URL -->
11021100
<?php echo $view['actions']->render(
1103-
$view['router']->generate(
1101+
// The url() method was introduced in Symfony 2.8. Prior to 2.8,
1102+
// you had to use generate($name, $parameters, UrlGeneratorInterface::ABSOLUTE_URL)
1103+
$view['router']->url(
11041104
'latest_news',
11051105
array('maxPerPage' => 5),
1106-
UrlGeneratorInterface::ABSOLUTE_URL
11071106
),
11081107
array('strategy' => 'esi'),
11091108
) ?>

Diff for: book/routing.rst

+15-10
Original file line numberDiff line numberDiff line change
@@ -1531,12 +1531,16 @@ a template helper function:
15311531

15321532
.. code-block:: html+php
15331533

1534-
<a href="<?php echo $view['router']->generate('blog_show', array(
1534+
<a href="<?php echo $view['router']->path('blog_show', array(
15351535
'slug' => 'my-blog-post',
15361536
)) ?>">
15371537
Read this blog post.
15381538
</a>
15391539

1540+
.. versionadded:: 2.8
1541+
The ``path()`` PHP templating helper was introduced in Symfony 2.8. Prior
1542+
to 2.8, you had to use the ``generate()`` helper method.
1543+
15401544
.. index::
15411545
single: Routing; Absolute URLs
15421546

@@ -1550,9 +1554,8 @@ method::
15501554
$this->generateUrl('blog_show', array('slug' => 'my-blog-post'), true);
15511555
// http://www.example.com/blog/my-blog-post
15521556

1553-
From a template, in Twig, simply use the ``url()`` function (which generates an absolute URL)
1554-
rather than the ``path()`` function (which generates a relative URL). In PHP, pass ``true``
1555-
to ``generate()``:
1557+
From a template, simply use the ``url()`` function (which generates an absolute
1558+
URL) rather than the ``path()`` function (which generates a relative URL):
15561559

15571560
.. configuration-block::
15581561

@@ -1564,16 +1567,18 @@ to ``generate()``:
15641567

15651568
.. code-block:: html+php
15661569

1567-
<?php
1568-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1569-
?>
1570-
1571-
<a href="<?php echo $view['router']->generate('blog_show', array(
1570+
<a href="<?php echo $view['router']->url('blog_show', array(
15721571
'slug' => 'my-blog-post',
1573-
), UrlGeneratorInterface::ABSOLUTE_URL) ?>">
1572+
)) ?>">
15741573
Read this blog post.
15751574
</a>
15761575

1576+
.. versionadded:: 2.8
1577+
The ``url()`` PHP templating helper was introduced in Symfony 2.8. Prior
1578+
to 2.8, you had to use the ``generate()`` helper method with
1579+
``Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL``
1580+
passed as third argument.
1581+
15771582
.. note::
15781583

15791584
The host that's used when generating an absolute URL is automatically

Diff for: book/templating.rst

+27-18
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,11 @@ tags:
709709
array('renderer' => 'hinclude')
710710
) ?>
711711
712+
<!-- The url() method was introduced in Symfony 2.8. Prior to 2.8, you
713+
had to use generate() with UrlGeneratorInterface::ABSOLUTE_URL
714+
passed as third argument. -->
712715
<?php echo $view['actions']->render(
713-
$view['router']->generate('...'),
716+
$view['router']->url('...'),
714717
array('renderer' => 'hinclude')
715718
) ?>
716719
@@ -918,7 +921,9 @@ To link to the page, just use the ``path`` Twig function and refer to the route:
918921

919922
.. code-block:: html+php
920923

921-
<a href="<?php echo $view['router']->generate('_welcome') ?>">Home</a>
924+
<!-- The path() method was introduced in Symfony 2.8. Prior to 2.8, you
925+
had to use generate(). -->
926+
<a href="<?php echo $view['router']->path('_welcome') ?>">Home</a>
922927

923928
As expected, this will generate the URL ``/``. Now, for a more complicated
924929
route:
@@ -997,7 +1002,9 @@ correctly:
9971002

9981003
<!-- app/Resources/views/Article/recent_list.html.php -->
9991004
<?php foreach ($articles in $article): ?>
1000-
<a href="<?php echo $view['router']->generate('article_show', array(
1005+
<!-- The path() method was introduced in Symfony 2.8. Prior to 2.8,
1006+
you had to use generate(). -->
1007+
<a href="<?php echo $view['router']->path('article_show', array(
10011008
'slug' => $article->getSlug(),
10021009
)) ?>">
10031010
<?php echo $article->getTitle() ?>
@@ -1006,26 +1013,26 @@ correctly:
10061013

10071014
.. tip::
10081015

1009-
You can also generate an absolute URL by using the ``url`` Twig function:
1016+
You can also generate an absolute URL by using the ``url`` function:
10101017

1011-
.. code-block:: html+twig
1018+
.. configuration-block::
10121019

1013-
<a href="{{ url('_welcome') }}">Home</a>
1020+
.. code-block:: html+twig
10141021

1015-
The same can be done in PHP templates by passing a third argument to
1016-
the ``generate()`` method:
1022+
<a href="{{ url('_welcome') }}">Home</a>
10171023

1018-
.. code-block:: html+php
1024+
.. code-block:: html+php
10191025

1020-
<?php
1021-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1022-
?>
1026+
<a href="<?php echo $view['router']->url(
1027+
'_welcome',
1028+
array()
1029+
) ?>">Home</a>
10231030

1024-
<a href="<?php echo $view['router']->generate(
1025-
'_welcome',
1026-
array(),
1027-
UrlGeneratorInterface::ABSOLUTE_URL
1028-
) ?>">Home</a>
1031+
.. versionadded:: 2.8
1032+
The ``url()`` PHP templating helper was introduced in Symfony 2.8. Prior
1033+
to 2.8, you had to use the ``generate()`` helper method with
1034+
``Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL``
1035+
passed as third argument.
10291036

10301037
.. index::
10311038
single: Templating; Linking to assets
@@ -1696,7 +1703,9 @@ key in the parameter hash:
16961703

16971704
.. code-block:: html+php
16981705

1699-
<a href="<?php echo $view['router']->generate('article_show', array(
1706+
<!-- The path() method was introduced in Symfony 2.8. Prior to 2.8, you
1707+
had to use generate(). -->
1708+
<a href="<?php echo $view['router']->path('article_show', array(
17001709
'id' => 123,
17011710
'_format' => 'pdf',
17021711
)) ?>">

Diff for: cookbook/security/csrf_in_login_form.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ using the login form:
107107
<!-- src/AppBundle/Resources/views/Security/login.html.php -->
108108

109109
<!-- ... -->
110-
<form action="<?php echo $view['router']->generate('login_check') ?>" method="post">
110+
<!-- The path() method was introduced in Symfony 2.8. Prior to 2.8, you
111+
had to use generate(). -->
112+
<form action="<?php echo $view['router']->path('login_check') ?>" method="post">
111113
<!-- ... the login fields -->
112114

113115
<input type="hidden" name="_csrf_token"

Diff for: cookbook/security/form_login.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ redirect to the URL defined by some ``account`` route, use the following:
253253
<div><?php echo $error->getMessage() ?></div>
254254
<?php endif ?>
255255

256-
<form action="<?php echo $view['router']->generate('login_check') ?>" method="post">
256+
<!-- The path() method was introduced in Symfony 2.8. Prior to 2.8, you
257+
had to use generate(). -->
258+
<form action="<?php echo $view['router']->path('login_check') ?>" method="post">
257259
<label for="username">Username:</label>
258260
<input type="text" id="username" name="_username" value="<?php echo $last_username ?>" />
259261

Diff for: cookbook/security/form_login_setup.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ Finally, create the template:
237237
<div><?php echo $error->getMessage() ?></div>
238238
<?php endif ?>
239239

240-
<form action="<?php echo $view['router']->generate('login_check') ?>" method="post">
240+
<!-- The path() method was introduced in Symfony 2.8. Prior to 2.8, you
241+
had to use generate(). -->
242+
<form action="<?php echo $view['router']->path('login_check') ?>" method="post">
241243
<label for="username">Username:</label>
242244
<input type="text" id="username" name="_username" value="<?php echo $last_username ?>" />
243245

Diff for: cookbook/security/impersonating_user.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ to show a link to exit impersonation:
8484
.. code-block:: html+php
8585

8686
<?php if ($view['security']->isGranted('ROLE_PREVIOUS_ADMIN')): ?>
87-
<a
88-
href="<?php echo $view['router']->generate('homepage', array(
89-
'_switch_user' => '_exit',
90-
) ?>"
91-
>
87+
<!-- The path() method was introduced in Symfony 2.8. Prior to 2.8, you
88+
had to use generate(). -->
89+
<a href="<?php echo $view['router']->path('homepage', array(
90+
'_switch_user' => '_exit',
91+
) ?>">
9292
Exit impersonation
9393
</a>
9494
<?php endif ?>

Diff for: cookbook/security/remember_me.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ this:
175175
<div><?php echo $error->getMessage() ?></div>
176176
<?php endif ?>
177177

178-
<form action="<?php echo $view['router']->generate('login_check') ?>" method="post">
178+
<!-- The path() method was introduced in Symfony 2.8. Prior to 2.8, you
179+
had to use generate(). -->
180+
<form action="<?php echo $view['router']->path('login_check') ?>" method="post">
179181
<label for="username">Username:</label>
180182
<input type="text" id="username"
181183
name="_username" value="<?php echo $last_username ?>" />

Diff for: cookbook/templating/PHP.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ updated by changing the configuration:
306306

307307
.. code-block:: html+php
308308

309-
<a href="<?php echo $view['router']->generate('hello', array('name' => 'Thomas')) ?>">
309+
<a href="<?php echo $view['router']->path('hello', array('name' => 'Thomas')) ?>">
310310
Greet Thomas!
311311
</a>
312312

313-
The ``generate()`` method takes the route name and an array of parameters as
313+
The ``path()`` method takes the route name and an array of parameters as
314314
arguments. The route name is the main key under which routes are referenced
315315
and the parameters are the values of the placeholders defined in the route
316316
pattern:

0 commit comments

Comments
 (0)