Question

I'm trying to figure what is the best way to design my urls. So here is what I have done so far:

account_index:
    pattern:  /Accounts/
    defaults: { _controller: "CoreBundle:Account:index" }
    requirements: { _method: get }

account_create:
    pattern:  /Accounts/
    defaults: { _controller: "CoreBundle:Account:create" }
    requirements: { _method: post }

account_read:
    pattern:  /Accounts/{id}
    defaults: { _controller: "CoreBundle:Account:show" }
    requirements: { _method: get }

account_update:
    pattern:  /Accounts/{id}
    defaults: { _controller: "CoreBundle:Account:update" }
    requirements: { _method: put }

account_delete:
    pattern:  /Accounts/{id}
    defaults: { _controller: "CoreBundle:Account:delete" }
    requirements: { _method: delete }

After testing what I've done, I realized that update and delete doesn't work (always calls account_read)... After googling my problem, I found out that PUT and DELETE methods aren't supported in all browsers... and may be dropped in the future.

I then read that Ruby on rails support these two methods on all browsers by doing some magic.

So I wonder, can Symfony2 handle PUT and DELETE like ruby does? AND Should I use restful url at all?

Était-ce utile?

La solution

Yes, symfony2 definitively is RESTful compliant.

I don't know for rails, but Symfony2 handles HTTP methods detection using different ways:

  • first get the REQUEST_METHOD from php
  • then if and only method == POST, gets the method from X-HTTP-METHOD-OVERRIDE header (fallback to _method request parameter)

Why it makes this chek on POST is because browsers can't send nothing else but GET or POST requests.

What is different from rails and/or symfony1 is that there is no helper to generate links or forms with corresponding methods. It's up to you to generate a valid request to your application.

PS: Concerning your routing, you should write your requirements on _method in UPPERCASE.

Autres conseils

If you are planning to implement RESTful services, you could take a look at RestBundle. They already have pretty good url naming conventions, that you could possibly agree with, and lots of other things that will simplify development process.

And yes, Symfony2 can handle PUT and DELETE requests.

The point is not what Symfony can do or can't, the point is how "Restful url" handle with current available browsers...

Some browsers wont allow you to use some HTTP methods ( like put or delete ), so yes, a good implementation will use GET, POST, PUT, DELETE and PATCH, but you will get issues with some users :)

As always... browsers...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top