Php 如何向Symfony服务添加构造函数参数

Php 如何向Symfony服务添加构造函数参数,php,symfony,service,symfony-3.3,Php,Symfony,Service,Symfony 3.3,我创建了一个使用FilesystemCache的服务,我不想每次调用该服务时都创建一个新的FilesystemCache,因此我在服务构造函数中有一个参数,可以在其中给出一个实例。 到目前为止,我所拥有的: 服务等级: class MyService { private $cache; private $url; /** * MyService constructor. * @param FilesystemCache $cache *

我创建了一个使用FilesystemCache的服务,我不想每次调用该服务时都创建一个新的FilesystemCache,因此我在服务构造函数中有一个参数,可以在其中给出一个实例。 到目前为止,我所拥有的:

服务等级

class MyService
{

    private $cache;

    private $url;

    /**
     * MyService constructor.
     * @param FilesystemCache $cache
     */
    public function __construct(FilesystemCache $cache)
    {
        $this->cache = $cache;
    }

    private function data()
    {
        if ($this->cache->has('data')) {
            $data = $this->cache->get('data');
        } else {
            $data = file_get_contents("my/url/to/data");
        }

        return $data;
    }
} 
services:
  # Aliases
  Symfony\Component\Cache\Adapter\FilesystemAdapter: '@cache.adapter.filesystem'

  # Services
  services.myservice:
    class: AppBundle\Services\MyService
    arguments:
      - '@cache.adapter.filesystem'
配置

class MyService
{

    private $cache;

    private $url;

    /**
     * MyService constructor.
     * @param FilesystemCache $cache
     */
    public function __construct(FilesystemCache $cache)
    {
        $this->cache = $cache;
    }

    private function data()
    {
        if ($this->cache->has('data')) {
            $data = $this->cache->get('data');
        } else {
            $data = file_get_contents("my/url/to/data");
        }

        return $data;
    }
} 
services:
  # Aliases
  Symfony\Component\Cache\Adapter\FilesystemAdapter: '@cache.adapter.filesystem'

  # Services
  services.myservice:
    class: AppBundle\Services\MyService
    arguments:
      - '@cache.adapter.filesystem'
我使用服务的地点:

$myService = $this->container->get('services.myservice');
但我得到的是一个错误:

The definition "services.myservice" has a reference to an abstract definition "cache.adapter.filesystem". Abstract definitions cannot be the target of references.

所以,我的问题是,我必须如何修改我的服务或声明,或者其他什么,才能完成我想做的事情:不要每次调用服务时都创建实例。

为了实现这一点,我必须向我想在服务构造函数中使用的类注册一个新服务。因此,我的
services.yml
如下所示:

services:
  filesystem.cache:
    class: Symfony\Component\Cache\Simple\FilesystemCache
  services.myservice:
    class: AppBundle\Services\MyService
    arguments:
      - '@filesystem.cache'

现在我可以使用我的服务而不会出错。

我强烈建议您使用
cache.app
服务,而不是您自己的
filesystem.cache
。此外,您还可以创建自己的适配器。

使用标准的S3.3 autowire设置,这对我很有用:

// services.yml

// This basically gives autowire a concrete cache implementation
// No additional parameters are needed
Symfony\Component\Cache\Simple\FilesystemCache:

// And there is no need for any entry for MyService


当然,只有当您的容器中只有一个CacheInterface的具体实现时,这才有效。

我希望您的参数是:
Symfony\Component\Cache\Adapter\FilesystemAdapter
而不是别名服务
@Cache.Adapter.filesystem
。就我看来,你应该创建一个新的FilesystemCache,因为它是一个抽象类,除非你扩展这个类,然后在constructor@dbrumann如果用路由替换别名,则会出现下一个错误:
依赖于不存在的服务“\Symfony\Component\Cache\Adapter\FilesystemAdapter”。
@YamenNassif FilesystemCache不是抽象类@piterio抱歉,这是我的错,您是否将其添加到AppKernel.php?请尝试新的Symfony\Component\Cache\Adapter\FilesystemAdapter(),而不是定义filesystem.cache,试着用类名替换@filesystem.cache。我想autowire会把它取出来。@Cerad我得到的:
Type错误:参数1传递给AppBundle\Services\MyService::\uu construct()必须是Symfony\Component\Cache\Simple\FilesystemCache的实例,给定字符串
如果我用@Cache.app替换我的@filesystem.Cache,我得到的是以下错误:
类型错误:参数1传递给AppBundle\Services\MyService::\uu construct()必须是Symfony\Component\Cache\Simple\FilesystemCache的实例,给定的Symfony\Component\Cache\Adapter\traceabledapter的实例