Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Does Symfony2 handle restful url properly and should I use restful url?

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

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

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

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

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

    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?

like image 865
Jean-Philippe Leclerc Avatar asked Feb 13 '12 01:02

Jean-Philippe Leclerc

1 Answers

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.

like image 79
Florian Klein Avatar answered Oct 05 '22 13:10

Florian Klein