Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new extra_information cookbook #780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions cookbook/routing/extra_information.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.. index::
single: Routage; Informations simplémentaires

Comment passer des informations d'une route à un contrôleur
===========================================================

Les paramètres présents à l'intérieur de la collection ``defaults`` ne
doivent pas spécialement être en rapport avec un élément du ``path``. En
fait, vous pouvez utiliser le tableau ``defaults`` pour ajouter des
paramètres supplémentaires qui sont accessible depuis votre contrôleur.

.. configuration-block::

.. code-block:: yaml

# app/config/routing.yml
blog:
path: /blog/{page}
defaults:
_controller: AppBundle:Blog:index
page: 1
title: "Hello world!"

.. code-block:: xml

<!-- app/config/routing.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="blog" path="/blog/{page}">
<default key="_controller">AppBundle:Blog:index</default>
<default key="page">1</default>
<default key="title">Hello world!</default>
</route>
</routes>

.. 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' => 'AppBundle:Blog:index',
'page' => 1,
'title' => 'Hello world!',
)));

return $collection;

Vous avez maintenant accès à un paramètre supplémentaire dans votre
contrôleur::

public function indexAction($page, $title)
{
// ...
}

Comme vous pouvez le constater, la variable ``$title`` n'a jamais été définie
dans le path de la route, mais vous pouvez tout de même accéder à sa valeur
dans votre contrôleur.
1 change: 1 addition & 0 deletions cookbook/routing/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Routage
service_container_parameters
custom_route_loader
redirect_trailing_slash
extra_information