1

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"
9
  • Why not inject the parameter instead ? Otherwise this is "service_container" and not "container" for the service id. Commented Dec 10, 2014 at 14:34
  • Why would it be "service_conteiner"....? well this works sort of...now I get a different error message will add it in my question now... Commented Dec 10, 2014 at 14:38
  • Can you update your services.xml too Commented Dec 10, 2014 at 14:51
  • just did @ChristopheWillemsen Commented Dec 10, 2014 at 14:52
  • The container you want is not the ContainerBuilder but Container, use ContainerInterface in your controller. Second, your service's class is wrong, you should point to your controller class Commented Dec 10, 2014 at 14:54

2 Answers 2

1

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

plus one ,And you can remove the use statement for ContainerBuilder. Also if he really wants to inject the container, it should use ContainerInterface instead, the container builder is, as it says, for building the container and return a Container instance.
Thank you for your suggestion I would be more then happy to use either as long as they work, besides my xml file containing all the declared services does not like me using the <parameters> tag it hihglight them and the whole xml file breakes
0

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

}

1 Comment

All gd but my xml file ssays: Element parameters: not allowed here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.