Search code examples
phpsymfonypostroutessonata

How do I restrict an auto-generated route to accept only POST requests?


We have overridden Sonata's RegistrationController using Easy Extends. In our generated appDevProjectContainerUrlMatcher class, we now have the following lines:

        if ($pathinfo === '/password/reset') {
            return array (  '_controller' => 'Application\\Sonata\\UserBundle\\Controller\\RegistrationController::passwordResetAction',  '_route' => 'fos_user_password_reset',);
        }

I am able to open my customized RegistrationController class and see the following:

/**
 * @return RedirectResponse
 */
public function passwordResetAction()
{
...
}

Now the question: I want to make this action only accept POST requests. How do I do that if there is no route annotation already present? (I can't find anywhere where this route is being explicitly defined, excluding the auto-generated class mentioned above.)

===

Edit: This is in a Symfony 2.7 application.


Solution

  • you can verify if it's a Post request like :

    if ($request->isMethod('post')) { //    Uppercase request method:POST
        // your code
    }
    

    the symfony 2.7 doc said

    getMethod() Gets the request "intended" method.

    may be you can try this also :

    $request->getMethod()