2012-09-05 63 views
2

的Symfony2我想創造出具有相同的URL API說http://stackoverflow.com/profile但具有類似於POSTPUTGET,等我想基於方法類型做不同的動作不同的請求方法。一種方式是寫一個比較方法共同行動,並使用重定向到不同的操作具有相同的URL

$this->forward('anotherModule','anotherAction'); 

是否有任何其他的方法來檢查它的方法,然後重定向到不同的操作,而無需使用一個普通的動作重定向?

回答

3

在你routing.yml您可以定義不同的控制器爲同一模式基於請求方法

read: 
    pattern: /yourUrl 
    defaults: { _controller: your.controller:readAction } 
    requirements: 
     _method: GET 

write: 
    pattern: /yourUrl 
    defaults: { _controller: your.controller:writeAction } 
    requirements: 
     _method: POST 

或者,如果您使用註釋:

/** 
* @Route("/yourRoute", requirements={"_method" = "GET"}) 
*/ 
public function showAction($id) 
{ 
    ... 
} 

/** 
* @Route("/yourRoute", requirements={"_method" = "POST"}) 
*/ 
public function writeAction($id) 
{ 
    ... 
} 
相關問題