@@ -7,7 +7,7 @@ How to Pass Extra Information from a Route to a Controller
7
7
Parameters inside the ``defaults `` collection don't necessarily have to
8
8
match a placeholder in the route ``path ``. In fact, you can use the
9
9
``defaults `` array to specify extra parameters that will then be accessible as
10
- arguments to your controller:
10
+ arguments to your controller, and as attributes of the `` Request `` object :
11
11
12
12
.. configuration-block ::
13
13
@@ -52,12 +52,25 @@ arguments to your controller:
52
52
53
53
return $collection;
54
54
55
- Now, you can access this extra parameter in your controller::
55
+ Now, you can access this extra parameter in your controller, as an argument to the controller method ::
56
56
57
+ use Symfony\Component\HttpFoundation\Request;
58
+
57
59
public function indexAction($page, $title)
58
60
{
59
61
// ...
60
62
}
61
63
64
+ Alternately, the title could be accessed through the ``Request `` object::
65
+
66
+ use Symfony\Component\HttpFoundation\Request;
67
+
68
+ public function indexAction(Request $request, $page)
69
+ {
70
+ // ...
71
+ $title = $request->attributes->get('title');
72
+ // ...
73
+ }
74
+
62
75
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.
76
+ but you can still access its value from inside your controller, through the method's argument, or from the `` Request `` object's `` attributes `` bag .
0 commit comments