@@ -815,10 +815,10 @@ Adding HTTP Method Requirements
815
815
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
816
816
817
817
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:
822
822
823
823
.. configuration-block ::
824
824
@@ -830,39 +830,39 @@ be accomplished with the following route configuration:
830
830
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
831
831
// ...
832
832
833
- class MainController extends Controller
833
+ class BlogApiController extends Controller
834
834
{
835
835
/**
836
- * @Route("/news ")
837
- * @Method("GET")
836
+ * @Route("/api/posts/{id} ")
837
+ * @Method({ "GET","HEAD"} )
838
838
*/
839
- public function newsAction( )
839
+ public function showAction($id )
840
840
{
841
- // ... display your news
841
+ // ... return a JSON response with the post
842
842
}
843
843
844
844
/**
845
- * @Route("/contact ")
846
- * @Method({"GET", "POST"} )
845
+ * @Route("/api/posts/{id} ")
846
+ * @Method("PUT" )
847
847
*/
848
- public function contactFormAction( )
848
+ public function editAction($id )
849
849
{
850
- // ... display and process a contact form
850
+ // ... edit a post
851
851
}
852
852
}
853
853
854
854
.. code-block :: yaml
855
855
856
856
# 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 ]
861
861
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 ]
866
866
867
867
.. code-block :: xml
868
868
@@ -873,12 +873,12 @@ be accomplished with the following route configuration:
873
873
xsi : schemaLocation =" http://symfony.com/schema/routing
874
874
http://symfony.com/schema/routing/routing-1.0.xsd" >
875
875
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 >
878
878
</route >
879
879
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 >
882
882
</route >
883
883
</routes >
884
884
@@ -889,24 +889,25 @@ be accomplished with the following route configuration:
889
889
use Symfony\Component\Routing\Route;
890
890
891
891
$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' )));
895
895
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 ')));
899
899
900
900
return $collection;
901
901
902
902
.. versionadded :: 2.2
903
903
The ``methods `` option was introduced in Symfony 2.2. Use the ``_method ``
904
904
requirement in older versions.
905
905
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.
910
911
911
912
.. note ::
912
913
0 commit comments