stefun
stefun

Reputation: 1551

Redirect to different Actions in symfony2 having same url

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

Answers (1)

Vitalii Zurian
Vitalii Zurian

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

Related Questions