可以在 SF3 中注释 Controller 操作以使用特定的“内容类型”吗?
我正在尝试放弃 '/api/post/{id}/xml' 路由黑客。
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class BlogApiController extends Controller
{
/**
* @Route("/api/posts/{id}")
* @Method({"GET","HEAD"})
* some-magic-annotation-here
*/
public function showJson($id)
{
// response in json
}
/**
* @Route("/api/posts/{id}")
* @Method({"GET","HEAD"})
* some-magic-annotation-here
*/
public function showXml($id)
{
// response in XML
}
}
最佳答案
据 official documentation您可以使用 条件 参数:
/**
* @Route(
* "/api/posts/{id}",
* condition="request.headers.get('Content-Type') === 'application/json'"
* )
* @Method({"GET","HEAD"})
*/
public function showJson($id)
{
...
XML 也是一样。
在 条件 您必须使用 expression syntax
关于Symfony 3 限制 Controller Action 以使用特定的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37348068/