From 45994ef8dfafbb692cdf1bf6b9f69740cc3e60b5 Mon Sep 17 00:00:00 2001 From: Sorin Dumitrescu Date: Tue, 2 Feb 2016 18:20:53 +0200 Subject: [PATCH 1/2] Point that route parameters are also Request attributes Point that route parameters are also Request attributes --- cookbook/routing/extra_information.rst | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cookbook/routing/extra_information.rst b/cookbook/routing/extra_information.rst index dbbe7706c46..ac8d8e25db5 100644 --- a/cookbook/routing/extra_information.rst +++ b/cookbook/routing/extra_information.rst @@ -7,7 +7,7 @@ How to Pass Extra Information from a Route to a Controller Parameters inside the ``defaults`` collection don't necessarily have to match a placeholder in the route ``path``. In fact, you can use the ``defaults`` array to specify extra parameters that will then be accessible as -arguments to your controller: +arguments to your controller, and as attributes of the ``Request`` object: .. configuration-block:: @@ -52,12 +52,16 @@ arguments to your controller: return $collection; -Now, you can access this extra parameter in your controller:: +Now, you can access this extra parameter in your controller, either as an argument (if specified), or through the ``Request`` object:: - public function indexAction($page, $title) + use Symfony\Component\HttpFoundation\Request; + + public function indexAction(Request $request, $page, $title) { // ... + $titleAttribute = $request->attributes->get('title'); // same as $title + // ... } As you can see, the ``$title`` variable was never defined inside the route path, -but you can still access its value from inside your controller. +but you can still access its value from inside your controller, or from the ``Request`` object's ``attributes`` bag. From 736b591bf5d972fd2da3efa4962c7b4383d92d8e Mon Sep 17 00:00:00 2001 From: Sorin Dumitrescu Date: Mon, 8 Feb 2016 14:10:59 +0200 Subject: [PATCH 2/2] Separated the two examples Separated the Request attributes example from the method argument one. --- cookbook/routing/extra_information.rst | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/cookbook/routing/extra_information.rst b/cookbook/routing/extra_information.rst index ac8d8e25db5..f34c94e32cf 100644 --- a/cookbook/routing/extra_information.rst +++ b/cookbook/routing/extra_information.rst @@ -52,16 +52,25 @@ arguments to your controller, and as attributes of the ``Request`` object: return $collection; -Now, you can access this extra parameter in your controller, either as an argument (if specified), or through the ``Request`` object:: +Now, you can access this extra parameter in your controller, as an argument to the controller method:: use Symfony\Component\HttpFoundation\Request; - public function indexAction(Request $request, $page, $title) + public function indexAction($page, $title) { // ... - $titleAttribute = $request->attributes->get('title'); // same as $title + } + +Alternately, the title could be accessed through the ``Request`` object:: + + use Symfony\Component\HttpFoundation\Request; + + public function indexAction(Request $request, $page) + { + // ... + $title = $request->attributes->get('title'); // ... } As you can see, the ``$title`` variable was never defined inside the route path, -but you can still access its value from inside your controller, or from the ``Request`` object's ``attributes`` bag. +but you can still access its value from inside your controller, through the method's argument, or from the ``Request`` object's ``attributes`` bag.