Home > PHP, Symfony 1.3 & 1.4 > symfony restful interface setup

symfony restful interface setup

How to make a restful interface easily with symfony:

# “project_root”/app/”app_name”/config/routing.yml

# default rules
homepage:
  url:   /
  param: { module: test, action: index }

test_get:
  url: /v2/tester
  class: sfRequestRoute
  param: { module: test, action: get }
  requirements:
    sf_method: [get]

test_put:
  url: /v2/tester
  class: sfRequestRoute
  param: { module: test, action: put }
  requirements:
    sf_method: [put]

test_post:
  url: /v2/tester
  class: sfRequestRoute
  param: { module: test, action: post }
  requirements:
    sf_method: [post]

test_head:
  url: /v2/tester
  class: sfRequestRoute
  param: { module: test, action: head }
  requirements:
    sf_method: [head]

# generic rules
# please, remove them by adding more specific rules
default_index:
  url:   /:module
  param: { action: index }

default:
  url:   /:module/:action/*

Remember, order DOES matter… if you have the default one at the top, the more specific matches to be overlooked. So move more specific items to the top, and your routing will be fine.

Summary:
post to http://localhost/sf_project/frontend_dev.php/test, will call frontend->test->post

delete to http://localhost/sf_project/frontend_dev.php/test, will call frontend->test->delete

put to http://localhost/sf_project/frontend_dev.php/test, will call frontend->test->put

head to http://localhost/sf_project/frontend_dev.php/test, will call frontend->test->head

you get the point…

  1. Sean Villani
    May 20th, 2010 at 09:36 | #1

    Please not the only acceptable Methods for symfony (as of 1.4.4):
    GET
    PUT
    DELETE
    POST
    HEAD

    all others will be defaulted to a GET.

  1. No trackbacks yet.