Skip to content

Commit 7dff788

Browse files
committed
minor #6062 Update HTTP method requirement example (WouterJ)
This PR was merged into the 2.3 branch. Discussion ---------- Update HTTP method requirement example | Q | A | --- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.3+ | Fixed tickets | #5583 After many reports (#5779, #5659) of the text no longer matching the example, I think it's time to update the example. The example now uses an API, which is a very common thing now and often needs this feature. API's are already used in the Page Creation article, which is placed before this article, so it shouldn't be too hard to understand. I know that from a REST prespective, HEAD and GET using the same action doesn't seem that great, but it's the only action I could think of that may support 2 methods. Commits ------- 30c2750 Update method requirement example
2 parents b39e369 + 30c2750 commit 7dff788

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

book/routing.rst

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -815,10 +815,10 @@ Adding HTTP Method Requirements
815815
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
816816

817817
In addition to the URL, you can also match on the *method* of the incoming
818-
request (i.e. GET, HEAD, POST, PUT, DELETE). Suppose you have a contact form
819-
with two controllers - one for displaying the form (on a GET request) and one
820-
for processing the form when it's submitted (on a POST request). This can
821-
be accomplished with the following route configuration:
818+
request (i.e. GET, HEAD, POST, PUT, DELETE). Suppose you create an API for
819+
your blog and you have 2 routes: One for displaying a post (on a GET or HEAD
820+
request) and one for updating a post (on a PUT request). This can be
821+
accomplished with the following route configuration:
822822

823823
.. configuration-block::
824824

@@ -830,39 +830,39 @@ be accomplished with the following route configuration:
830830
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
831831
// ...
832832
833-
class MainController extends Controller
833+
class BlogApiController extends Controller
834834
{
835835
/**
836-
* @Route("/news")
837-
* @Method("GET")
836+
* @Route("/api/posts/{id}")
837+
* @Method({"GET","HEAD"})
838838
*/
839-
public function newsAction()
839+
public function showAction($id)
840840
{
841-
// ... display your news
841+
// ... return a JSON response with the post
842842
}
843843
844844
/**
845-
* @Route("/contact")
846-
* @Method({"GET", "POST"})
845+
* @Route("/api/posts/{id}")
846+
* @Method("PUT")
847847
*/
848-
public function contactFormAction()
848+
public function editAction($id)
849849
{
850-
// ... display and process a contact form
850+
// ... edit a post
851851
}
852852
}
853853
854854
.. code-block:: yaml
855855
856856
# app/config/routing.yml
857-
news:
858-
path: /news
859-
defaults: { _controller: AppBundle:Main:news }
860-
methods: [GET]
857+
api_show_post:
858+
path: /api/posts/{id}
859+
defaults: { _controller: AppBundle:BlogApi:show }
860+
methods: [GET, HEAD]
861861
862-
contact_form:
863-
path: /contact
864-
defaults: { _controller: AppBundle:Main:contactForm }
865-
methods: [GET, POST]
862+
api_edit_post:
863+
path: /api/posts/{id}
864+
defaults: { _controller: AppBundle:BlogApi:edit }
865+
methods: [PUT]
866866
867867
.. code-block:: xml
868868
@@ -873,12 +873,12 @@ be accomplished with the following route configuration:
873873
xsi:schemaLocation="http://symfony.com/schema/routing
874874
http://symfony.com/schema/routing/routing-1.0.xsd">
875875
876-
<route id="news" path="/news" methods="GET">
877-
<default key="_controller">AppBundle:Main:news</default>
876+
<route id="api_show_post" path="/api/posts/{id}" methods="GET|HEAD">
877+
<default key="_controller">AppBundle:BlogApi:show</default>
878878
</route>
879879
880-
<route id="contact_form" path="/contact" methods="GET|POST">
881-
<default key="_controller">AppBundle:Main:contactForm</default>
880+
<route id="api_edit_post" path="/api/posts/{id}" methods="PUT">
881+
<default key="_controller">AppBundle:BlogApi:edit</default>
882882
</route>
883883
</routes>
884884
@@ -889,24 +889,25 @@ be accomplished with the following route configuration:
889889
use Symfony\Component\Routing\Route;
890890
891891
$collection = new RouteCollection();
892-
$collection->add('news', new Route('/news', array(
893-
'_controller' => 'AppBundle:Main:contact',
894-
), array(), array(), '', array(), array('GET')));
892+
$collection->add('api_show_post', new Route('/api/posts/{id}', array(
893+
'_controller' => 'AppBundle:BlogApi:show',
894+
), array(), array(), '', array(), array('GET', 'HEAD')));
895895
896-
$collection->add('contact_form', new Route('/contact', array(
897-
'_controller' => 'AppBundle:Main:contactForm',
898-
), array(), array(), '', array(), array('GET', 'POST')));
896+
$collection->add('api_edit_post', new Route('/api/posts/{id}', array(
897+
'_controller' => 'AppBundle:BlogApi:edit',
898+
), array(), array(), '', array(), array('PUT')));
899899
900900
return $collection;
901901
902902
.. versionadded:: 2.2
903903
The ``methods`` option was introduced in Symfony 2.2. Use the ``_method``
904904
requirement in older versions.
905905

906-
Despite the fact that these two routes have identical paths (``/contact``),
907-
the first route will match only GET requests and the second route will match
908-
only POST requests. This means that you can display the form and submit the
909-
form via the same URL, while using distinct controllers for the two actions.
906+
Despite the fact that these two routes have identical paths
907+
(``/api/posts/{id}``), the first route will match only GET or HEAD requests and
908+
the second route will match only PUT requests. This means that you can display
909+
and edit the post with the same URL, while using distinct controllers for the
910+
two actions.
910911

911912
.. note::
912913

0 commit comments

Comments
 (0)