在从Symfony 2.7升级到3.0.2之后,我注意到crud生成器的控制器发生了变化。
Symfony 2样本:
/**
* Finds and displays a Article entity.
*
* @Route("/{id}", name="article_show")
* @Method("GET")
* @Template("AppBundle:article:show.html.twig")
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Article')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Article entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}Symfony 3样本:
/**
* Finds and displays a Article entity.
*
* @Route("/{id}", name="article_show")
* @Method("GET")
*/
public function showAction(Article $article)
{
$deleteForm = $this->createDeleteForm($article);
return $this->render('article/show.html.twig', array(
'article' => $article,
'delete_form' => $deleteForm->createView(),
));
}不知道这到底是什么时候发生了变化,因为我在使用版本2.8时没有使用crud生成器。
总之,我感兴趣的神奇之处是:
public function showAction(Article $article)它似乎与以前的版本相同:
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Article')->find($id);
...
}我在Symfony网站上找不到有关这方面的任何文件。有人能解释一下这个特性是如何工作的吗?我在哪里可以找到更多的信息?它只对实体有效,还是.?
谢谢!
发布于 2016-02-24 20:36:49
这个特性叫做ParamConverter -在这里阅读更多:http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
在控制器中没有@ParamConverter注释,因为:
If you use type hinting as in the example above, you can even omit the @ParamConverter annotation altogether
https://stackoverflow.com/questions/35612491
复制相似问题