我知道有人讨论了在 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 一起使用的方式吗?