diff --git a/book/routing.rst b/book/routing.rst index 3b82cb3033a..ac5398fa2c6 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -1032,6 +1032,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 539c1217538..24a0cd78371 100644 --- a/cookbook/map.rst.inc +++ b/cookbook/map.rst.inc @@ -134,6 +134,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