Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ba293dd

Browse filesBrowse files
committed
Merge branch '2.3' into 2.4
* 2.3: Clarify that route defaults don't need a placeholder
2 parents fdeab33 + d5d46ec commit ba293dd
Copy full SHA for ba293dd

File tree

Expand file treeCollapse file tree

4 files changed

+69
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+69
-0
lines changed

‎book/routing.rst

Copy file name to clipboardExpand all lines: book/routing.rst
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,10 @@ see :ref:`route-parameters-controller-arguments`.
10431043
You can also use a special ``$_route`` variable, which is set to the
10441044
name of the route that was matched.
10451045

1046+
You can even add extra information to your route definition and access it
1047+
within your controller. For more information on this topic,
1048+
see :doc:`/cookbook/routing/extra_information`.
1049+
10461050
.. index::
10471051
single: Routing; Importing routing resources
10481052

‎cookbook/map.rst.inc

Copy file name to clipboardExpand all lines: cookbook/map.rst.inc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
* :doc:`/cookbook/routing/service_container_parameters`
135135
* :doc:`/cookbook/routing/custom_route_loader`
136136
* :doc:`/cookbook/routing/redirect_trailing_slash`
137+
* :doc:`/cookbook/routing/extra_information`
137138

138139
* :doc:`/cookbook/security/index`
139140

+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. index::
2+
single: Routing; Extra Information
3+
4+
How to Pass Extra Information from a Route to a Controller
5+
==========================================================
6+
7+
Parameters inside the ``defaults`` collection don't necessarily have to
8+
match a placeholder in the route ``path``. In fact, you can use the
9+
``defaults`` array to specify extra parameters that will then be accessible as
10+
arguments to your controller:
11+
12+
.. configuration-block::
13+
14+
.. code-block:: yaml
15+
16+
# app/config/routing.yml
17+
blog:
18+
path: /blog/{page}
19+
defaults:
20+
_controller: AcmeBlogBundle:Blog:index
21+
page: 1
22+
title: "Hello world!"
23+
24+
.. code-block:: xml
25+
26+
<!-- app/config/routing.xml -->
27+
<?xml version="1.0" encoding="UTF-8" ?>
28+
<routes xmlns="http://symfony.com/schema/routing"
29+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
30+
xsi:schemaLocation="http://symfony.com/schema/routing
31+
http://symfony.com/schema/routing/routing-1.0.xsd">
32+
33+
<route id="blog" path="/blog/{page}">
34+
<default key="_controller">AcmeBlogBundle:Blog:index</default>
35+
<default key="page">1</default>
36+
<default key="title">Hello world!</default>
37+
</route>
38+
</routes>
39+
40+
.. code-block:: php
41+
42+
// app/config/routing.php
43+
use Symfony\Component\Routing\RouteCollection;
44+
use Symfony\Component\Routing\Route;
45+
46+
$collection = new RouteCollection();
47+
$collection->add('blog', new Route('/blog/{page}', array(
48+
'_controller' => 'AcmeBlogBundle:Blog:index',
49+
'page' => 1,
50+
'title' => 'Hello world!',
51+
)));
52+
53+
return $collection;
54+
55+
Now, you can access this extra parameter in your controller::
56+
57+
public function indexAction($page, $title)
58+
{
59+
// ...
60+
}
61+
62+
As you can see, the ``$title`` variable was never defined inside the route path,
63+
but you can still access its value from inside your controller.

‎cookbook/routing/index.rst

Copy file name to clipboardExpand all lines: cookbook/routing/index.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Routing
1111
service_container_parameters
1212
custom_route_loader
1313
redirect_trailing_slash
14+
extra_information

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.