Reputation: 1551
I want create API which has same URL say http://stackoverflow.com/profile
but having different request method like POST
, PUT
, GET
, etc. I want to do different action based on method type. One way is write common action which for comparing methods and redirect using
$this->forward('anotherModule','anotherAction');
Is there any other way to check it's method and redirect to different actions without using one general action?
Upvotes: 2
Views: 1135
Reputation: 17976
In your routing.yml
you can define different controllers for the same pattern based on request method
read:
pattern: /yourUrl
defaults: { _controller: your.controller:readAction }
requirements:
_method: GET
write:
pattern: /yourUrl
defaults: { _controller: your.controller:writeAction }
requirements:
_method: POST
Or if you use annotations:
/**
* @Route("/yourRoute", requirements={"_method" = "GET"})
*/
public function showAction($id)
{
...
}
/**
* @Route("/yourRoute", requirements={"_method" = "POST"})
*/
public function writeAction($id)
{
...
}
Upvotes: 3