Symfony的3.4至4.2模拟@ParamConverter类?

问题描述 投票:-1回答:2

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

  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)

symfony symfony4
2个回答
0
投票

相同的方式,你ParamConverter做这件事是Symfony3。 SensioFrameworkExtraBundle不会被弃用。


0
投票

由于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的一个属性(因此,使用相同的名称(“身份证”在这里))。

© www.soinside.com 2019 - 2024. All rights reserved.