James Kirkwood
James Kirkwood

Reputation: 69

Route is incorrectly associated with redirect controller

I'm attempting to design a RESTful interface in Symfony which will be called through AJAX requests. I've been having a problem where my POST method route is being matched to the built-in redirect controller instead of the one I created below:

/**
 * @Route("/todos")
 */
class TodoController extends Controller
{
    /**
     * @Route("/", name="todos")
     * @Method("GET")
     */
    public function indexAction()
    {
        // Get action here
    }

    /**
     * @Route("/{id}", name="todo_delete")
     * @Method("DELETE")
     */
    public function deleteAction($id)
    {
    // Delete action here
    }

    /**
     * @Route("/", name="todo_create")
     * @Method({"POST"})
     */
    public function createAction()
    { 
        return new Response("Hello!");
    }
}

My indexAction and deleteAction work fine, but my createAction did not. When I looked at the logs this is what I saw:

[2011-10-24 19:27:14] request.INFO: Matched route "todo_create" (parameters: "_controller": "Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction", "path": "/todos/", "permanent": "true", "scheme": "null", "httpPort": "80", "httpsPort": "443", "_route": "todo_create") [] []

It looks like my route is not even being associated with the TodoController I created. Any idea what is causing this?

Upvotes: 1

Views: 1168

Answers (2)

James Kirkwood
James Kirkwood

Reputation: 69

I figured it out. It turns out my client side code was calling "http://todos.localhost/todos" where the routes were expecting "http://todos.localhost/todos/" (which has a trailing /). I removed the slash in my POST request route as follows

/**
 * @Route("", name="todo_create")
 * @Method({"POST"})
 */

and everything works fine.

It seems like Symfony realized that the slash was missing, added it to the request url, and performed an internal redirect on using the new url. When Symfony performed the redirect, however, it wasn't maintaining the request method (POST in this case). Instead it was calling my GET controller.

Upvotes: 0

Stefan Gehrig
Stefan Gehrig

Reputation: 83622

---> @Method({"POST"})

Shouldn't that be

@Method("POST")

?

Upvotes: 1

Related Questions