Search code examples
phpsymfonyrouteshttp-method

How to create 2 actions with same path but different http methods Symfony2


I work with symfony routing annotation and I have already set the http_method_override to true

I want to create two different actions but with a different behaviour according to the http method like this:

/**
 * Event controller.
 *
 * @Route("/event")
 */
class EventController extends Controller
{

    /**
     * Lists all Event entities.
     *
     * @Route("/", name="event")
     * @Method("GET")
     * @Template() // default template (index.html.twig)
     */
    public function indexAction()
    {
       ...
    }

    /**
     * Creates a new Event entity.
     *
     * @Route("/", name="event_create")
     * @Method("POST")
     * @Template("...") // a special template new.html.twig
     */
    public function createAction(Request $request)
    {
        ...
    }

But when I try to access to /event/, there is a 405 page saying :

No route found for "GET /event/": Method Not Allowed (Allow: POST)

And when I try to list my routes with php app/console router:debug :

  event_create             POST   ANY    ANY  /event/                                      
  event                    GET    ANY    ANY  /event/week/{timestamp}                      
  event_new                GET    ANY    ANY  /event/new
  event_show               GET    ANY    ANY  /event/{id}                                  
  event_edit               GET    ANY    ANY  /event/{id}/edit
  event_update             PUT    ANY    ANY  /event/{id}
  event_delete             DELETE ANY    ANY  /event/{id} 

Solution

  • I'm sorry, I've just realized reading my question that an other method below with the path /event/week/{timestamp} was named "event" too :/

    So I renamed this method to event_week and it works.