@@ -812,10 +812,10 @@ Adding HTTP Method Requirements
812812~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
813813
814814In 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:
819819
820820.. configuration-block ::
821821
@@ -827,39 +827,39 @@ be accomplished with the following route configuration:
827827 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
828828 // ...
829829
830- class MainController extends Controller
830+ class BlogApiController extends Controller
831831 {
832832 /**
833- * @Route("/news ")
834- * @Method("GET")
833+ * @Route("/api/posts/{id} ")
834+ * @Method({ "GET","HEAD"} )
835835 */
836- public function newsAction( )
836+ public function showAction($id )
837837 {
838- // ... display your news
838+ // ... return a JSON response with the post
839839 }
840840
841841 /**
842- * @Route("/contact ")
843- * @Method({"GET", "POST"} )
842+ * @Route("/api/posts/{id} ")
843+ * @Method("PUT" )
844844 */
845- public function contactFormAction( )
845+ public function editAction($id )
846846 {
847- // ... display and process a contact form
847+ // ... edit a post
848848 }
849849 }
850850
851851 .. code-block :: yaml
852852
853853 # 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 ]
858858
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 ]
863863
864864 .. code-block :: xml
865865
@@ -870,12 +870,12 @@ be accomplished with the following route configuration:
870870 xsi : schemaLocation =" http://symfony.com/schema/routing
871871 http://symfony.com/schema/routing/routing-1.0.xsd" >
872872
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 >
875875 </route >
876876
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 >
879879 </route >
880880 </routes >
881881
@@ -886,20 +886,21 @@ be accomplished with the following route configuration:
886886 use Symfony\Component\Routing\Route;
887887
888888 $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' )));
892892
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 ')));
896896
897897 return $collection;
898898
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.
903904
904905.. note ::
905906
0 commit comments