Skip to content

Commit 6f854f9

Browse files
committed
[Book][Routing] Add example about how to match multiple methods
1 parent 318bb8a commit 6f854f9

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

book/routing.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,67 @@ form via the same URL, while using distinct controllers for the two actions.
910910

911911
If no ``methods`` are specified, the route will match on *all* methods.
912912

913+
A route can also match multiple methods. This is useful if you want to
914+
handle form presentation and processing in a single action for example.
915+
916+
.. configuration-block::
917+
918+
.. code-block:: php-annotations
919+
920+
// src/AppBundle/Controller/MainController.php
921+
namespace AppBundle\Controller;
922+
923+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
924+
// ...
925+
926+
class MainController extends Controller
927+
{
928+
/**
929+
* @Route("/new")
930+
* @Method({"GET", "POST"})
931+
*/
932+
public function newAction()
933+
{
934+
// ... display or process form
935+
}
936+
937+
}
938+
939+
.. code-block:: yaml
940+
941+
# app/config/routing.yml
942+
new:
943+
path: /new
944+
defaults: { _controller: AppBundle:Main:new }
945+
methods: [GET, POST]
946+
947+
.. code-block:: xml
948+
949+
<!-- app/config/routing.xml -->
950+
<?xml version="1.0" encoding="UTF-8" ?>
951+
<routes xmlns="http://symfony.com/schema/routing"
952+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
953+
xsi:schemaLocation="http://symfony.com/schema/routing
954+
http://symfony.com/schema/routing/routing-1.0.xsd">
955+
956+
<route id="new" path="/new" methods="GET|POST">
957+
<default key="_controller">AppBundle:Main:new</default>
958+
</route>
959+
</routes>
960+
961+
.. code-block:: php
962+
963+
// app/config/routing.php
964+
use Symfony\Component\Routing\RouteCollection;
965+
use Symfony\Component\Routing\Route;
966+
967+
$collection = new RouteCollection();
968+
$collection->add('new', new Route('/new', array(
969+
'_controller' => 'AppBundle:Main:new',
970+
), array(), array(), '', array(), array('GET', 'POST')));
971+
972+
return $collection;
973+
913974
Adding a Host Requirement
914975
~~~~~~~~~~~~~~~~~~~~~~~~~
915976

0 commit comments

Comments
 (0)