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 d5d46ec

Browse filesBrowse files
committed
feature #4017 Clarify that route defaults don't need a placeholder (iamdto)
This PR was merged into the 2.3 branch. Discussion ---------- Clarify that route defaults don't need a placeholder | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.3+ | Fixed tickets | #3960 Commits ------- a18e5cc Clarify that route defaults don't need a placeholder
2 parents efc1436 + a18e5cc commit d5d46ec
Copy full SHA for d5d46ec

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
@@ -955,6 +955,10 @@ see :ref:`route-parameters-controller-arguments`.
955955
You can also use a special ``$_route`` variable, which is set to the
956956
name of the route that was matched.
957957

958+
You can even add extra information to your route definition and access it
959+
within your controller. For more information on this topic,
960+
see :doc:`/cookbook/routing/extra_information`.
961+
958962
.. index::
959963
single: Routing; Importing routing resources
960964

‎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
@@ -128,6 +128,7 @@
128128
* :doc:`/cookbook/routing/service_container_parameters`
129129
* :doc:`/cookbook/routing/custom_route_loader`
130130
* :doc:`/cookbook/routing/redirect_trailing_slash`
131+
* :doc:`/cookbook/routing/extra_information`
131132

132133
* :doc:`/cookbook/security/index`
133134

+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.