2016-10-03 103 views
1

我对symfony2有个奇怪的问题。 在service.yml我宣布分页服务:Symfony服务声明命名空间

site.paginate_service: 
class: "Smestaj\SiteBundle\Services\PaginationService" 
arguments: 
    - "@service_container" 

服务看起来是这样的:

namespace Smestaj\SiteBundle\Services; 
use Symfony\Component\DependencyInjection\ContainerInterface; 
class PaginationService{ 

protected $cn; 
protected $numberOfData; 

public function __construct(ContainerInterface $container) 
{ 
    $this->cn = $container; 
    $this->numberOfData = $container->getParameter("limit")['adsPerPage']; 
} 

问题是,当我称之为service.yml这种服务到另一个服务为dependacy注射

site.ads_service: 
class: "Smestaj\SiteBundle\Services\AdsService" 
arguments: 
    - "@doctrine.orm.entity_manager" 
    - "@service_container" 
calls: 
    - [setPaginate, ["@site.paginate_service"]] 

然后我得到这个错误信息:

试图从命名空间“Smestaj \ SiteBundle”加载类“ServicesaginationService”。 你忘记了另一个命名空间的“使用”语句吗?

所以,从这个消息可以清楚看出,symfony试图调用类“ServicesaginationService”。我的班级有Smestaj \ SiteBundle \ Services \ PaginationService。 Symfony以某种方式合并服务和PaginationService名称,并从名称中删除“P”。

如果我将类名更改为AaaService,那么一切正常。

+0

尝试从'class'值中删除引号。 –

+0

将service_container注入到服务本身是一个糟糕的主意。你应该只注入依赖的服务,参数等,但不是服务容器 – rokas

回答

1

当您在service.yml使用类名双引号,你必须通过添加另一个\逃离\

class: "Smestaj\\SiteBundle\\Services\\PaginationService" 

但为了避免问题的最好办法是删除引号,因为路径被YML解析器理解为字符串:

class: Smestaj\SiteBundle\Services\PaginationService 
+0

谢谢你的回应。你的答案解决了我的问题 –