Tomazi
Tomazi

Reputation: 847

Symfony2 inject a service into a controller/class

I am working one a simfony2 project and trying to inject ConteinerBuilder into one of my classes so I can use the getParameter() function to retrive info from parameters.yml file.

My class set up:

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');
    }
}

My serviices.xml file set up:

<?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 Error Message:

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

I have many other services declared in this file they all work fine but not this one....does anyone see what I am doing wrong..?

OK after a suggestion to change service id from "container" to "service_conteiner" the above error message has dissapired but new one appears

New error message.

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

Upvotes: 1

Views: 533

Answers (2)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

As I said before: INJECT THE PARAMETERS, otherwise

From my comments :

-- 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');
    }

}

Upvotes: 0

Rooneyl
Rooneyl

Reputation: 7902

As Christophe says, you are better off injectiong the patameter you required.
Something like;

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>

Class;

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;
    }

}

Upvotes: 1

Related Questions