symfony4中的软件包注入

时间:2018-11-14 07:49:17

标签: php symfony4

问题是我通过名为carbon的作曲家安装了一个软件包,并将其用作控制器构造函数中的依赖项注入,当我运行我的项目时,它向我显示:

Invalid service "Carbon\Carbon": method "__construct()" has no argument named "$carbon". Check your service definition.

这是我的控制器:

class IndexController extends AbstractController
{
    private $carbon;

    public function __construct(Carbon $carbon)
    {
        $this->carbon = $carbon;
    }

}

这是我的services.yml,我定义了carbon类,然后将其用作参数:

services:
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false        # Allows optimizing the container by removing unused services; this also means
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    Carbon\Carbon:
        $carbon: 'Carbon/Carbon'
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']
        arguments: ['@Carbon\Carbon']

解决此问题的最佳方法是什么!!

1 个答案:

答案 0 :(得分:0)

从您的services.yml中删除此行

<ul class="tags">


    <?php

    $tags = get_tags('post_tag'); //taxonomy=post_tag
    //echo "<pre>";
    //print_r($tags);
    //var_dump($tags);
    if ( $tags ) :
        foreach ( $tags as $tag ) : ?>
            <li><a class="tag" href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>