【问题标题】:Symfony 3.3 autoconfigure services and usageSymfony 3.3 自动配置服务和使用
【发布时间】:2017-07-15 16:24:35
【问题描述】:

我将我的项目更新到 Symfony 3.3。我想为服务使用新的自动配置功能。我试图摆脱$this->get(),但我在控制器和命令中有错误。

在控制器中使用下面的代码示例,我有这个错误:

recapitulatifCollesAction() requires that you provide a value for the "$checkGele" 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.

在命令中,我根本不知道如何摆脱$container->get()。

你知道我怎样才能让它工作吗?

控制器:

public function recapitulatifCollesAction($estEnCours, CheckGeleService $checkGele)
{
    // ...
    $checkGele->getGeleAutorisation($colle);
    // ...
}

我的配置:

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

编辑:修改 config.yml 后出现新错误

【问题讨论】:

    标签: symfony symfony-3.3


    【解决方案1】:

    对于控制器,您还需要将服务参数解析器添加到“操作”方法中的自动装配服务中。这就是 3.3 中的默认自动装配:

    services:
        _defaults:
            autowire: true
            autoconfigure: true
            public: false
    
        AppBundle\:
            resource: '../../src/AppBundle/*'
            exclude: '../../src/AppBundle/{Controller, Entity, Repository}'
    
        AppBundle\Controller\:
            resource: '../../src/AppBundle/Controller'
            public: true
            tags: [ controller.service_arguments ]
    

    【讨论】:

    • 在我的配置中出现 500 错误。调试起来真的很困难,因为我在日志中看不到任何内容...
    • 我有一个新的错误消息:The autoloader expected class "AppBundle\Resources\public\ckeditor\samples\old\assets\posteddata" to be defined in file "root\vendor\composer/../../src\AppBundle\Resources\public\ckeditor\samples\old\assets\posteddata.php". The file was found but the class was not in it, the class name or namespace probably has a typo in root\app/config\config.yml. 我检查了这个文件甚至不是应该加载的类。这是一个普通的php文件。所以我排除了 Resources 文件夹从服务中导入,但我仍然有同样的错误
    • 然后发生了一件奇怪的事情:这个文件posteddata.php 在我进入主页时被加载,所以我在 Symfony 错误页面的顶部有它的内容(检查更新的第一篇文章)
    • 您的Resources/public 文件夹似乎包含一些第三方lib PHP 文件(此处为posteddata.ph‌​p)。您需要通过在AppBundle\: 条目中设置此exclude: '../../src/AppBundle/{Controller, Entity, Repository, Resources}',将它们从服务自动配置中排除。
    猜你喜欢
    • 1970-01-01
    • 2018-01-17
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    相关资源
    最近更新 更多