symfony 表单生成器是否使用容器?

Does symfony form builder use container?

由于代码较大,我将我的文件放在 gist 中。

描述:

我的 api 捆绑包的

BankController 监听来自银行的 POST 请求。当控制器获得所需的变量时,它会尝试使用 HalykType 构建 HalykEntity 表单。

如果一切正常且表单已验证,控制器将尝试调用 HalykEntity 的 makeOperation 方法,该方法取决于操作类型 (Funding/Refunding) 调用适当的方法 (makeFunding/makeRefunding)。

它们(makeFunding/makeRefunding 方法)都可能调用 Banking bundle 的 Banking 服务方法。

问题:

当我尝试模拟来自银行的呼叫时(发送 post 请求 ..AMOUNT=5201&CURRENCY=KZT&TIMESTAMP=20170906173539&TRTYPE=1..) 我收到错误:

{
"error": {
    "code": 500,
    "message": "Internal Server Error",
    "exception": [
        {
            "message": "Call to a member function get() on null",
            "class": "Symfony\Component\Debug\Exception\FatalThrowableError",
            "trace": [
                {
                    "namespace": "",
                    "short_class": "",
                    "class": "",
                    "type": "",
                    "function": "",
                    "file": "src\ApiBundle\v1\Entity\HalykEntity.php",
                    "line": 308,
                    "args": []
                },
                {
                    "namespace": "ApiBundle\v1\Entity",
                    "short_class": "HalykEntity",
                    "class": "ApiBundle\v1\Entity\HalykEntity",
                    "type": "->",
                    "function": "makeFunding",
                    "file": "src\ApiBundle\v1\Entity\HalykEntity.php",
                    "line": 274,
                    "args": []
                },
                {
                    "namespace": "ApiBundle\v1\Entity",
                    "short_class": "HalykEntity",
                    "class": "ApiBundle\v1\Entity\HalykEntity",
                    "type": "->",
                    "function": "makeOperation",
                    "file": "src\ApiBundle\v1\Controller\BankController.php",
                    "line": 34,
                    "args": []
                },
                {
                    "namespace": "ApiBundle\v1\Controller",
                    "short_class": "BankController",
                    "class": "ApiBundle\v1\Controller\BankController",
                    "type": "->",
                    "function": "indexAction",
                    "file": null,
                    "line": null,
                    "args": [
                        [
                            "object",
                            "Symfony\Component\HttpFoundation\Request"
                        ]
                    ]
                },
                {
                    "namespace": "",
                    "short_class": "",
                    "class": "",
                    "type": "",
                    "function": "call_user_func_array",
                    "file": "vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php",
                    "line": 153,
                    "args": [
                        [
                            "array",
                            [
                                [
                                    "object",
                                    "ApiBundle\v1\Controller\BankController"
                                ],
                                [
                                    "string",
                                    "indexAction"
                                ]
                            ]
                        ],
                        [
                            "array",
                            [
                                [
                                    "object",
                                    "Symfony\Component\HttpFoundation\Request"
                                ]
                            ]
                        ]
                    ]
                },
                {
                    "namespace": "Symfony\Component\HttpKernel",
                    "short_class": "HttpKernel",
                    "class": "Symfony\Component\HttpKernel\HttpKernel",
                    "type": "->",
                    "function": "handleRaw",
                    "file": "vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php",
                    "line": 68,
                    "args": [
                        [
                            "object",
                            "Symfony\Component\HttpFoundation\Request"
                        ],
                        [
                            "integer",
                            1
                        ]
                    ]
                },
                {
                    "namespace": "Symfony\Component\HttpKernel",
                    "short_class": "HttpKernel",
                    "class": "Symfony\Component\HttpKernel\HttpKernel",
                    "type": "->",
                    "function": "handle",
                    "file": "vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php",
                    "line": 171,
                    "args": [
                        [
                            "object",
                            "Symfony\Component\HttpFoundation\Request"
                        ],
                        [
                            "integer",
                            1
                        ],
                        [
                            "boolean",
                            true
                        ]
                    ]
                },
                {
                    "namespace": "Symfony\Component\HttpKernel",
                    "short_class": "Kernel",
                    "class": "Symfony\Component\HttpKernel\Kernel",
                    "type": "->",
                    "function": "handle",
                    "file": "web\app_dev.php",
                    "line": 29,
                    "args": [
                        [
                            "object",
                            "Symfony\Component\HttpFoundation\Request"
                        ]
                    ]
                }
            ]
        }
    ]
}

}

想法: 我认为问题在于表单生成器不使用 DI 容器。我错了吗?如果没有,如何调用银行服务方法?

P.S.

抱歉,我没有足够的声誉来放置更多 2 个链接。所以,我必须删除一些指向 类 的链接。但是你可以在我的 gist repo 中找到所有这些。

我决定重构我的架构。

原因:

  1. 当我只需要传递银行服务时,为什么要将整个容器传递给我的实体
  2. 根据最佳做法传递整个容器不是好的做法。 (我找不到 link 文档,但我记得容器实例确实很大,需要大量内存)

解法:

在我的实体 class 中,makeFunding/makeRefunding 方法需要银行服务实例

function makeFunding( IApiBanking $banking ): bool {
    return $banking->makeFunding();
}

在我的控制器中,我只是调用实体方法,例如:

/** @var HalykEntity $entity */
$entity = $form->getData();
$view   = $this->view( [
    'success' => $entity->makeOperation( $this->get( 'banking' ) )
], 200 )->setTemplate( "ApiBundle:v1:Resources:views:layouts:default.html.twig" );