Symfony 3.0 attribute 'factory-service' and 'factory-method' is not allowed
最近使用 Symfony3.0 开发项目的时候,发现在之前的 Symfony 2.x 版本的,在注入服务的时候可以使用下列方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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"> <parameters> <parameter key="ks_user.user_repository.class">Kernelstudio\Bundle\UserBundle\Repository\UserRepository</parameter> </parameters> <services> <!-- 2.x 版本中可以使用 factory-service 和 factory-method 属性 --> <service id="ks_user.user_repository" factory-service="doctrine.orm.entity_manager" factory-method="getRepository" class="%ks_user.user_repository.class%"> <argument>%ks_user.entity.user.class%</argument> </service> </services> </container> |
结果到了使用 Symfony 3.0 的时候,使用同样的方法进行注入的时候,会报一下错误:
1 2 3 4 5 6 7 | InvalidArgumentException in XmlUtils.php line 96: [ERROR 1866] Element '{http://symfony.com/schema/dic/services}service', attribute 'factory-service': The attribute 'factory-service' is not allowed. (in file:///E:/webroot/blog/web/ - line 19, column 0) [ERROR 1866] Element '{http://symfony.com/schema/dic/services}service', attribute 'factory-method': The attribute 'factory-method' is not allowed. (in file:///E:/webroot/blog/web/ - line 19, column 0) |
结果仔细看文档,才发现 3.0版本已经没有这种写法了,需要做如下调整 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?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"> <parameters> <parameter key="ks_user.user_repository.class">Kernelstudio\Bundle\UserBundle\Repository\UserRepository</parameter> </parameters> <services> <!-- 3.0 版本的写法 --> <service id="ks_user.user_repository" class="%ks_user.user_repository.class%"> <!-- 3.0 需要使用这个标签 --> <factory service="doctrine.orm.entity_manager" method="getRepository" /> <argument>%ks_user.entity.user.class%</argument> </service> <!-- 2.x 版本的写法 <service id="ks_user.user_repository" factory-service="doctrine.orm.entity_manager" factory-method="getRepository" class="%ks_user.user_repository.class%"> <argument>%ks_user.entity.user.class%</argument> </service> --> </services> </container> |