我正在开发一个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"
的建议之后,确定以上错误消息已失效但出现新消息
新错误消息。
"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"
答案 0 :(得分:1)
正如克里斯托夫所说,你最好注射你需要的拍拍者 喜欢的东西;
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;
}
}
答案 1 :(得分:0)
正如我之前所说:注入参数,否则
来自我的评论:
-- 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');
}
}