Symfony 3.4 到 4.2 模拟@ParamConverter类



这是一种非常方便的方法。

  use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
    ....     
        /**
         * @Route("/test/{id_object}", name="test")
         * @ParamConverter("ObjectEntity", class="Bundle:ObjectEntity", options={"id" = "id_object"})
         */
        public function editTest(ObjectEntity $ObjectEntity, Request $request) {
    .....
    }

现在如何进行参数转换?(交响乐4)

从 Symfony 4.2 开始,要使用 ParamConverter,您不必使用注释@ParamConverter而是直接引用实体的类型提示。

所以

use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
.....
    /**
     * @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"("test/{id}")必须是ObjectEntity的一个属性。(因此,请使用相同的名称(此处为"id"))。

就像

你在Symfony3上使用ParamConverter所做的一样。SensioFrameworkExtraBundle 未弃用。

最新更新