簡體   English   中英

Symfony2將服務注入控制器/類

[英]Symfony2 inject a service into a controller/class

我正在一個simfony2項目中嘗試將ConteinerBuilder注入到我的一個類中,因此我可以使用getParameter()函數從p arameters.yml文件中檢索信息。

我的課程設置:

namespace NewsInfrastructure\Sitemap;

use NewsInfrastructure\DatabaseRepository;
use Symfony\Component\DependencyInjection\Container;

class DbSitemapReadRepository extends DatabaseRepository
{

 protected $container;

 /**
     * @Route(service="parameters.container")
     * @param Container $Container
     */
public function __construct(Container $Container)
    {
        $this->container = $Container;
    }

 public function getRootURL()
    {

      $this->container->getParameter('sitemap_root_url');
    }
}

我的serviices.xml文件設置為:

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<service id="parameters.container"
         class="NewsInfrastructure\Sitemap\DbSitemapReadRepository">
    <argument type="service" id="service_container" />
</service>

Symfony 2錯誤消息:

The service "parameters.container" has a dependency on e non-existing service "container"

我在此文件中聲明了許多其他服務,它們都可以正常工作,但不能正常工作..有人看到我在做什么錯嗎?

在將服務ID從"container"更改為"service_conteiner"的建議后,單擊“確定"service_conteiner" ,上面的錯誤消息已消失,但出現了新的錯誤消息

新的錯誤消息。

"Catchable Fatal Error: Argument 1 passed to NewsInfrastucture\Sitemap\MyController::__construct() must be an instance of \Symfony\Component\DependencyInjection\ConteinerBuilder, Instance of Doctrine\DBAL\Connection given"

正如Christophe所說,最好注入所需的參數。
就像是;

service.xml中

<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        http://symfony.com/schema/dic/services/services-1.0.xsd"
>
    <services>
        <parameters>
        <parameter key="sitemap_root_url">foo</parameter>
    </parameters>

    <services>
        <service id="DbSitemapReadRepository" class="Acme\HelloBundle\NewsInfrastructure\Sitemap\DbSitemapReadRepository">
            <argument>%sitemap_root_url%</argument>
        </service>
    </services>
    </services>
</container>

類;

namespace NewsInfrastructure\Sitemap;

use NewsInfrastructure\DatabaseRepository;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class DbSitemapReadRepository extends DatabaseRepository
{

 protected $siteMapUrl;

   /**
     * @param ContainerBuilder $ContainerBuilder
     */
    public function __construct($sitemap_root_url)
    {
        $this->siteMapUrl = $sitemap_root_url;
    }

}

正如我之前所說:注射參數,否則

根據我的評論:

-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        http://symfony.com/schema/dic/services/services-1.0.xsd"
>

    <services>
        <service id="parameters.container" class="NewsInfrastructure\Sitemap\DbSitemapReadRepository">
            <argument type="service" name="service_container"/>
        </service>
    </services>
</container>

X

namespace NewsInfrastructure\Sitemap;

use NewsInfrastructure\DatabaseRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DbSitemapReadRepository extends DatabaseRepository
{

 protected $siteMapUrl;

   /**
     * @Route(service="parameters.container")
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->siteMapUrl = $container->getParameter('sitemap_url');
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM