Php Symfony服务声明命名空间
我对symfony2有奇怪的问题。 在service.yml中,我声明了分页服务:Php Symfony服务声明命名空间,php,symfony,service,dependency-injection,Php,Symfony,Service,Dependency Injection,我对symfony2有奇怪的问题。 在service.yml中,我声明了分页服务: site.paginate_service: class: "Smestaj\SiteBundle\Services\PaginationService" arguments: - "@service_container" 服务如下所示: namespace Smestaj\SiteBundle\Services; use Symfony\Component\DependencyInjection\Cont
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中将此服务作为依赖注入调用到另一个服务中时
site.ads_service:
class: "Smestaj\SiteBundle\Services\AdsService"
arguments:
- "@doctrine.orm.entity_manager"
- "@service_container"
calls:
- [setPaginate, ["@site.paginate_service"]]
然后我得到这个错误消息:
试图加载“类”服务
名称空间“Smestaj\SiteBundle”中的“aginationService”。
您是否忘记了另一个名称空间的“use”语句
因此,从这条消息中可以明显看出,symfony试图将类称为“ServicesAgiantionService”。我的类有Smestaj\SiteBundle\Services\PaginationService。
Symfony,以某种方式合并服务和分页服务名称,并从名称中删除“P”
如果我将类名更改为AaaService,那么一切都正常 当您在
service.yml
的类名中使用双引号时,您必须通过添加另一个\
来转义\
:
class: "Smestaj\\SiteBundle\\Services\\PaginationService"
但避免问题的最佳方法是删除引号,因为YML解析器会将路径正确解释为字符串:
class: Smestaj\SiteBundle\Services\PaginationService
尝试从
class
值中删除引号。将服务容器注入服务本身是个坏主意。您应该只注入依赖服务、参数等,而不是服务容器谢谢您的响应。你的回答解决了我的问题