From a18e5cce0ea7ee95ad37e2dbaa5e3e9d61c6bf36 Mon Sep 17 00:00:00 2001 From: Denis Togbe Date: Fri, 11 Jul 2014 12:08:50 +0200 Subject: [PATCH] Clarify that route defaults don't need a placeholder --- book/routing.rst | 4 ++ cookbook/map.rst.inc | 1 + cookbook/routing/extra_information.rst | 63 ++++++++++++++++++++++++++ cookbook/routing/index.rst | 1 + 4 files changed, 69 insertions(+) create mode 100644 cookbook/routing/extra_information.rst diff --git a/book/routing.rst b/book/routing.rst index 3607721a043..6dbaf109cae 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -955,6 +955,10 @@ see :ref:`route-parameters-controller-arguments`. You can also use a special ``$_route`` variable, which is set to the name of the route that was matched. +You can even add extra information to your route definition and access it +within your controller. For more information on this topic, +see :doc:`/cookbook/routing/extra_information`. + .. index:: single: Routing; Importing routing resources diff --git a/cookbook/map.rst.inc b/cookbook/map.rst.inc index 749756b09de..32b751c60d5 100644 --- a/cookbook/map.rst.inc +++ b/cookbook/map.rst.inc @@ -128,6 +128,7 @@ * :doc:`/cookbook/routing/service_container_parameters` * :doc:`/cookbook/routing/custom_route_loader` * :doc:`/cookbook/routing/redirect_trailing_slash` + * :doc:`/cookbook/routing/extra_information` * :doc:`/cookbook/security/index` diff --git a/cookbook/routing/extra_information.rst b/cookbook/routing/extra_information.rst new file mode 100644 index 00000000000..c2b29421219 --- /dev/null +++ b/cookbook/routing/extra_information.rst @@ -0,0 +1,63 @@ +.. index:: + single: Routing; Extra Information + +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: + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/routing.yml + blog: + path: /blog/{page} + defaults: + _controller: AcmeBlogBundle:Blog:index + page: 1 + title: "Hello world!" + + .. code-block:: xml + + + + + + + AcmeBlogBundle:Blog:index + 1 + Hello world! + + + + .. code-block:: php + + // app/config/routing.php + use Symfony\Component\Routing\RouteCollection; + use Symfony\Component\Routing\Route; + + $collection = new RouteCollection(); + $collection->add('blog', new Route('/blog/{page}', array( + '_controller' => 'AcmeBlogBundle:Blog:index', + 'page' => 1, + 'title' => 'Hello world!', + ))); + + return $collection; + +Now, you can access this extra parameter in your controller:: + + public function indexAction($page, $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. diff --git a/cookbook/routing/index.rst b/cookbook/routing/index.rst index 526839e77c5..c42cff1748d 100644 --- a/cookbook/routing/index.rst +++ b/cookbook/routing/index.rst @@ -11,3 +11,4 @@ Routing service_container_parameters custom_route_loader redirect_trailing_slash + extra_information