路由器如何实例化类?

时间:2019-05-24 20:51:44

标签: php symfony routing

我正在更改Controller类的__construct()方法的必需参数。将参数添加到类构造函数后,将不会加载使用该控制器的页面。

这是一个几乎没有文档的应用程序,而我最近才被聘请从事该应用程序的工作。我是一位经验丰富的开发人员,但被投入到一个Symfony项目中,而我从未参与过该项目。

简单地说,我的问题是,我需要使用最初从Docker容器配置加载到Symfony类的配置选项。首席开发人员坚持将这种配置注入到类中。好吧。

但是,当我将其他参数添加到__construct()方法时,我开始收到错误,这也是我所期望的。但是我不知道在哪里可以找到实例化调用?

这是类定义的开头,它显示了在构造方法上添加了三个新参数:

class DocTreeController extends AbstractController
{
    use CommandDispatcherTrait;

    private const ETAG_SEED = '1';

    /**
     * @var ClientInterface
     */
    private $guzzleJsonClient;

    private $caseNetworkSecret;
    private $caseNetworkScope;
    private $caseTokenServer;

    /**
     * @var PdoAdapter
     */
    private $externalDocCache;

    public function __construct(ClientInterface $guzzleJsonClient, PdoAdapter $externalDocCache, ?string $caseNetworkSecret, ?string $caseNetworkScope, ?string $caseTokenServer)
    {
        $this->guzzleJsonClient = $guzzleJsonClient;
        $this->externalDocCache = $externalDocCache;
        $this->caseNetworkSecret = $caseNetworkSecret;
        $this->caseTokenServer = $caseTokenServer;
        $this->caseNetworkScope = $caseNetworkScope;
    }

根据注释中应用于此的路由,这是索引函数的缩写版本:

    /**
     * @Route("/doc/{slug}.{_format}", name="doc_tree_view", methods={"GET"}, defaults={"_format"="html", "lsItemId"=null})
     * @Route("/doc/{slug}/av.{_format}", name="doc_tree_view_av", methods={"GET"}, defaults={"_format"="html", "lsItemId"=null})
     * @Route("/doc/{slug}/lv.{_format}", name="doc_tree_view_log", methods={"GET"}, defaults={"_format"="html", "lsItemId"=null})
     * @Route("/doc/{slug}/{assocGroup}.{_format}", name="doc_tree_view_ag", methods={"GET"}, defaults={"_format"="html", "lsItemId"=null})
     * @Entity("lsDoc", expr="repository.findOneBySlug(slug)")
     * @Template()
     */
    public function viewAction(LsDoc $lsDoc, AuthorizationCheckerInterface $authChecker, ?UserInterface $user = null, $_format = 'html', $lsItemId = null, $assocGroup = null)
    {
        $em = $this->getDoctrine()->getManager();

        ....

        return $ret;
    }

并且基于Symfony文档中一些无效的绊脚石,这是我尝试在services.yaml文件中创建的条目,以某种方式认为它必须是服务:

    # Doc Controller
    App\Controller\Framework\DocTreeController:
        class: App\Controller\Framework\DocTreeController
        arguments:
            $guzzleJsonClient: '@csa_guzzle.client.json'
            $externalDocCache: '@salt.cache.external_docs'
            $caseNetworkSecret: '%case_network_secret%'
            $caseNetworkScope: '%case_network_scope%'
            $caseTokenServer: '%case_token_server%'

当我什至转到索引时,都会收到以下错误消息:

[2019-05-24 20:00:37] request.CRITICAL: Uncaught PHP Exception ArgumentCountError: "Too few arguments to function App\Controller\Framework\DocTreeController::__construct(), 2 passed in /var/www/html/var/cache/prod/ContainerAcbnxwG/getDocTreeControllerService.php on line 14 and exactly 5 expected" at /var/www/html/src/Controller/Framework/DocTreeController.php line 58 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Too few arguments to function App\\Controller\\Framework\\DocTreeController::__construct(), 2 passed in /var/www/html/var/cache/prod/ContainerAcbnxwG/getDocTreeControllerService.php on line 14 and exactly 5 expected at /var/www/html/src/Controller/Framework/DocTreeController.php:58)"} []

让我感到沮丧的是,错误消息指向的似乎是动态生成的缓存文件,而不是框架中的实际文档。

感谢这里的人们有足够的帮助而离开!

0 个答案:

没有答案