symfony表单构建器是否使用容器?

时间:2017-09-14 11:12:28

标签: php symfony symfony-3.3

由于代码较大,我将文件放到gist

说明

我的api包的

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

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

他们两个(makeFunding / makeRefunding方法)可能会调用Banking捆绑的银行服务方法。

问题:

当我尝试模拟来自银行的来电时(发送帖子请求..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个链接。所以,我必须删除一些类的链接。但是你可以在我的要点中找到所有这些。

1 个答案:

答案 0 :(得分:1)

我决定重构我的架构。

原因:

  1. 当我只需要传递银行服务时,为什么要将整个容器传递给我的实体
  2. 根据最佳实践传递整个容器并不是一个好习惯。 (我无法找到doc的链接,但我记得容器实例非常庞大,需要大量内存)
  3. <强>解决方案:

    在我的Entity类中,makeFunding / makeRefunding方法需要Banking服务实例

    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" );