Symfony 4 - 在路由中使用简单的实体注入器 [英] Symfony 4 - use simple entity injector in route

查看:28
本文介绍了Symfony 4 - 在路由中使用简单的实体注入器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Symfony 4.0,我尝试使用我的路由注入我的实体.

Using Symfony 4.0, I try to inject my entity using my route.

这是我的控制器:

   /**
     * @param TblResidence $tblResidence
     * @return \Symfony\Component\HttpFoundation\Response
     * @Route("/residences/{res_id}", name="residences_view")
     */
    public function view(TblResidence $tblResidence)
    {
        return $this->render('residences/view.html.twig', [
            'residence' => $tblResidence,
        ]);
    }

这是我的实体:

/**
 * TblResidence
 *
 * @ORM\Table(name="tbl_residence", indexes={@ORM\Index(name="syn_id", columns={"syn_id"}), @ORM\Index(name="uti_id_crea", columns={"uti_id_crea"}), @ORM\Index(name="uti_id_maj", columns={"uti_id_maj"}), @ORM\Index(name="exp_id", columns={"exp_id"}), @ORM\Index(name="res_code", columns={"res_code"}), @ORM\Index(name="mar_id", columns={"mar_id"}), @ORM\Index(name="not_id", columns={"not_id"}), @ORM\Index(name="seg_id", columns={"seg_id"})})
 * @ORM\Entity
 */
class TblResidence
{
    /**
     * @var int
     *
     * @ORM\Column(name="res_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $resId;

    ....

现在我遇到了这个错误:

And for now I got this error :

控制器App\Controller\ResidencesController::view()"要求您为$tblResidence"参数提供了一个值.无论是参数可为空且未提供空值,无默认值已提供值或因为存在非可选参数在这之后.

Controller "App\Controller\ResidencesController::view()" requires that you provide a value for the "$tblResidence" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

我认为我不需要使用 ParamConverter(老实说,我没有找到有关它的 SF4 文档),因为我只想使用 ID 进行匹配.我试过 resId, id, res_id 作为参数名,还是一样的错误.

I don't think I need to use ParamConverter (to be honest, I didn't find docs for SF4 about it) since I just want to match using the ID. I tried resId, id, res_id as param name, still same error.

我是否错过了什么(是的,我当然会错过)?

Do I miss something (yes I do of course) ?

编辑

我从 composer 安装了这个包:

I installed this package from composer :

composer require sensio/framework-extra-bundle

现在它可以工作了,ParamConverter 似乎依赖于这个包.

And now it works, the ParamConverter seems to depend on this package.

更多信息:https://symfony.com/doc/current/捆绑包/SensioFrameworkExtraBundle/index.html

推荐答案

您的参数必须像在您的实体中一样被称为 resId.如果你想使用不同的名字,你必须明确地使用@ParamConverter 注释:https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter

Your parameter have to be called resId like in your entity. If you want to use a different name, you have to be explicit with the @ParamConverter annotation: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter

/**
 * @param TblResidence $tblResidence
 * @return \Symfony\Component\HttpFoundation\Response
 * @Route("/residences/{resId}", name="residences_view")
 */
public function view(TblResidence $tblResidence = null)
{
    return $this->render('residences/view.html.twig', [
        'residence' => $tblResidence,
    ]);
}

这篇关于Symfony 4 - 在路由中使用简单的实体注入器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆