我知道有什麼是處理Symfony2路由的最佳實踐(routing.yml vs annotations)的討論。只要讓我提一下,我想保持它的方式,使用註釋。如何在特定路線上使用多個方法註解?
當我定義在控制器的單個動作的多個路由,現在看來,是的@Method
註釋最後一個定義覆蓋所有其他與這就是爲什麼我收到以下錯誤:
No route found for "POST /index": Method Not Allowed (Allow: GET, HEAD)
這只是我正在使用的一小段代碼。
namespace MySelf\MyBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class MyController extends Controller{
/**
* @Route(
* "/index",
* name="index_default"
*)
* @Method({"GET", "POST"})
*
* @Route(
* "/index/{id}",
* name="index",
* requirements={
* "id": "\d+"
* }
*)
* @Method({"GET"})
*
* @return Response
*/
public function indexAction($id = null){
/*DO SOME FANCY STUFF*/
...
return $response;
}
}
雖然這工作得很好!
index_default:
pattern: /index
defaults: { _controller: MyBundle:MyController:index }
requirements:
_method: GET|POST
index:
pattern: /index/{id}
defaults: { _controller: MyBundle:MyController:index }
requirements:
_method: GET
id: \d+
任何想法,它實現它的方式,而不是使用註釋routing.yml?
嘗試直接在指定的路線標註方法(方法=「GET | POST」) – rpg600
謝謝,這不正是我希望得到:)只要它張貼作爲一個單獨的答案,所以我可以勾選這:) :) – nTOXIC