Symfony中的Autowire String参数

时间:2018-08-03 12:44:34

标签: php symfony

如何在Symfony 3.4中连接String参数?

我的服务很简单,我想连接url中指定的parameters.yml参数:

namespace AppBundle\Service;

use Psr\Log\LoggerInterface;

class PythonService {

    private $logger;
    private $url;

    /**
     * @param LoggerInterface $logger
     * @param String $url
     */
    public function __construct(LoggerInterface $logger, String $url) {
        $this->logger = $logger;
        $this->url = $url;
    }
}

我的service.yml如下:

AppBunde\Services\PythonService:
    arguments: ['@logger', '%url%']

但是我遇到了错误:

Cannot autowire service "AppBundle\Service\PythonService": argument "$url" of method "__construct()" is type-hinted "string", you should configure its value explicitly.

我也尝试过手动指定参数:

AnalyticsDashboardBunde\Services\PythonService:
    arguments:
        $logger: '@logger'
        $url: '%session_memcached_host%'

这给了我以下错误:

Invalid service "AppBundle\Services\PythonService": class "AppBundle\Services\PythonService" does not exist.

2 个答案:

答案 0 :(得分:6)

首先,您在AppBundle\Services\PythonService(服务<>服务)中有一个错字。

然后,字符串<>字符串。 php中没有大写字母。


您可以将参数绑定到某个参数/服务:

service.yml:

services:
    _defaults:
        bind:
            $memcacheHostUri: '%session_memcached_host%'

服务类别:(必须与指定的^相同的var名称)

public function __construct(LoggerInterface $logger, string $memcacheHostUri)

控制器操作:

public function myAwesomeAction(PythonService $pythonService)
{
    $pythonService->doPythonStuffs();
}

使用此解决方案,如果您创建需要memecacheHostUri的其他服务,则这些服务也将自动连线。

资源:

Argument binding

答案 1 :(得分:0)

var observable = new List<int> { 1, 1, 2, 2, 3, 3, 4, 1, 1, 5, 6, 7, 1, 1 }
    .ToObservable();

observable.StartWith(int.MinValue).Zip(observable, (previous, current) => (previous, current))
    .Where(t => t.current != 1 || t.current != t.previous)
    .Select(t => t.current);