@@ -812,10 +812,10 @@ Adding HTTP Method Requirements
812
812
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
813
813
814
814
In addition to the URL, you can also match on the *method * of the incoming
815
- request (i.e. GET, HEAD, POST, PUT, DELETE). Suppose you have a contact form
816
- with two controllers - one for displaying the form (on a GET request) and one
817
- for processing the form when it's submitted (on a POST request). This can
818
- be accomplished with the following route configuration:
815
+ request (i.e. GET, HEAD, POST, PUT, DELETE). Suppose you create an API for
816
+ your blog and you have 2 routes: One for displaying a post (on a GET or HEAD
817
+ request) and one for updating a post (on a PUT request). This can be
818
+ accomplished with the following route configuration:
819
819
820
820
.. configuration-block ::
821
821
@@ -827,39 +827,39 @@ be accomplished with the following route configuration:
827
827
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
828
828
// ...
829
829
830
- class MainController extends Controller
830
+ class BlogApiController extends Controller
831
831
{
832
832
/**
833
- * @Route("/news ")
834
- * @Method("GET")
833
+ * @Route("/api/posts/{id} ")
834
+ * @Method({ "GET","HEAD"} )
835
835
*/
836
- public function newsAction( )
836
+ public function showAction($id )
837
837
{
838
- // ... display your news
838
+ // ... return a JSON response with the post
839
839
}
840
840
841
841
/**
842
- * @Route("/contact ")
843
- * @Method({"GET", "POST"} )
842
+ * @Route("/api/posts/{id} ")
843
+ * @Method("PUT" )
844
844
*/
845
- public function contactFormAction( )
845
+ public function editAction($id )
846
846
{
847
- // ... display and process a contact form
847
+ // ... edit a post
848
848
}
849
849
}
850
850
851
851
.. code-block :: yaml
852
852
853
853
# app/config/routing.yml
854
- news :
855
- path : /news
856
- defaults : { _controller: AppBundle:Main:news }
857
- methods : [GET]
854
+ api_post_show :
855
+ path : /api/posts/{id}
856
+ defaults : { _controller: AppBundle:BlogApi:show }
857
+ methods : [GET, HEAD ]
858
858
859
- contact_form :
860
- path : /contact
861
- defaults : { _controller: AppBundle:Main:contactForm }
862
- methods : [GET, POST ]
859
+ api_post_edit :
860
+ path : /api/posts/{id}
861
+ defaults : { _controller: AppBundle:BlogApi:edit }
862
+ methods : [PUT ]
863
863
864
864
.. code-block :: xml
865
865
@@ -870,12 +870,12 @@ be accomplished with the following route configuration:
870
870
xsi : schemaLocation =" http://symfony.com/schema/routing
871
871
http://symfony.com/schema/routing/routing-1.0.xsd" >
872
872
873
- <route id =" news " path =" /news " methods =" GET" >
874
- <default key =" _controller" >AppBundle:Main:news </default >
873
+ <route id =" api_post_show " path =" /api/posts/{id} " methods =" GET|HEAD " >
874
+ <default key =" _controller" >AppBundle:BlogApi:show </default >
875
875
</route >
876
876
877
- <route id =" contact_form " path =" /contact " methods =" GET|POST " >
878
- <default key =" _controller" >AppBundle:Main:contactForm </default >
877
+ <route id =" api_post_edit " path =" /api/posts/{id} " methods =" PUT " >
878
+ <default key =" _controller" >AppBundle:BlogApi:edit </default >
879
879
</route >
880
880
</routes >
881
881
@@ -886,20 +886,21 @@ be accomplished with the following route configuration:
886
886
use Symfony\Component\Routing\Route;
887
887
888
888
$collection = new RouteCollection();
889
- $collection->add('news ', new Route('/news ', array(
890
- '_controller' => 'AppBundle:Main:contact ',
891
- ), array(), array(), '', array(), array('GET')));
889
+ $collection->add('api_post_show ', new Route('/api/posts/{id} ', array(
890
+ '_controller' => 'AppBundle:BlogApi:show ',
891
+ ), array(), array(), '', array(), array('GET', 'HEAD' )));
892
892
893
- $collection->add('contact_form ', new Route('/contact ', array(
894
- '_controller' => 'AppBundle:Main:contactForm ',
895
- ), array(), array(), '', array(), array('GET', 'POST ')));
893
+ $collection->add('api_post_edit ', new Route('/api/posts/{id} ', array(
894
+ '_controller' => 'AppBundle:BlogApi:edit ',
895
+ ), array(), array(), '', array(), array('PUT ')));
896
896
897
897
return $collection;
898
898
899
- Despite the fact that these two routes have identical paths (``/contact ``),
900
- the first route will match only GET requests and the second route will match
901
- only POST requests. This means that you can display the form and submit the
902
- form via the same URL, while using distinct controllers for the two actions.
899
+ Despite the fact that these two routes have identical paths
900
+ (``/api/posts/{id} ``), the first route will match only GET or HEAD requests and
901
+ the second route will match only PUT requests. This means that you can display
902
+ and edit the post with the same URL, while using distinct controllers for the
903
+ two actions.
903
904
904
905
.. note ::
905
906
0 commit comments