这是一个非常方便的方法。
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
....
/**
* @Route("/test/{id_object}", name="test")
* @ParamConverter("ObjectEntity", class="Bundle:ObjectEntity", options={"id" = "id_object"})
*/
public function editTest(ObjectEntity $ObjectEntity, Request $request) {
.....
}
现在怎么办参数的转换?(Symfony的4)
相同的方式,你ParamConverter做这件事是Symfony3。 SensioFrameworkExtraBundle不会被弃用。
由于Symfony的4.2,使用ParamConverter,你不可以使用注释@ParamConverter而是直接引用到你的实体类型提示。
所以
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
.....
/**
* @Route("/test/{id_object}", name="test")
* @ParamConverter("ObjectEntity", class="Bundle:ObjectEntity", options={"id" = "id_object"})
*/
public function editTest(ObjectEntity $ObjectEntity, Request $request) {
.....
}
变
/**
* @Route("/test/{id}", name="test")
*/
public function editTest(ObjectEntity $obj, Request $req) {
....
//A query is automatically runs to find the ObjectEntity which corresponds with the id sent in the Route
//so $obj is the ObjectEntity whose $id property matches the id value in the Route, else if id value in the Route doesn't match with the ObjectEntity's id, you will have a 404 page.
}
重要提示:在路径(“测试/(编号)”)的参数“ID”必须是ObjectEntity的一个属性(因此,使用相同的名称(“身份证”在这里))。