Symfony依赖注入和您自己的服务

时间:2014-02-25 09:59:37

标签: php symfony dependency-injection

我试图在symfony依赖注入组件的帮助下将我自己的类注册为服务,但是我在类加载方面遇到了问题。

我有这样的文件结构:

enter image description here

My Generator类很简单

<?php

namespace Localhost\Service\String;

class Generator {

    private $iStringLength;

    public function __construct($iNewStringLength = 5) {
        $this->iStringLength = $iNewStringLength;
    }

    public function getRandomString() {
        $sChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $sRandChar = substr(str_shuffle(str_repeat($sChars,5)),0, $this->iStringLength);

        return $sRandChar;
    }
}

索引是

<?php

require_once 'vendor/autoload.php';

/*
spl_autoload_register(function ($sClass) {
    echo $sClass;
    require_once str_replace('\\', '/', $sClass) . '.php';
});
*/

use Localhost\Service\String\Generator;

/*
$oStringGenerator = new Generator(55);
echo $oStringGenerator->getRandomString();
*/

use Symfony\Component\DependencyInjection\ContainerBuilder;

$oContainer = new ContainerBuilder();
$oContainer
    ->register('generator', 'Generator')
    ->addArgument('15');

$oGeneratorService = $oContainer->get('generator');
echo $oGeneratorService->getRandomString();

我得到的是一个错误

Fatal error: Uncaught exception 'ReflectionException' with message 'Class Generator does not exist' in D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php:959 Stack trace: #0 D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php(959): ReflectionClass->__construct('Generator') #1 D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php(493): Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object(Symfony\Component\DependencyInjection\Definition), 'generator') #2 D:\Localhost\Apache\htdocs\Test\index.php(26): Symfony\Component\DependencyInjection\ContainerBuilder->get('generator') #3 {main} thrown in D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php on line 959

或作为图片

enter image description here

4 个答案:

答案 0 :(得分:2)

解决方案很简单,我忘了修改composer配置来加载我的服务

"autoload": {
    "psr-0": {"Localhost": "src/"}
},

答案 1 :(得分:2)

$oContainer = new ContainerBuilder();
$oContainer
    ->register('generator', 'Localhost\Service\String\Generator')
    ->addArgument('15');

答案 2 :(得分:1)

编辑:自Symfony 3.3 + (2017年5月)以来,您可以使用register()班级名称服务快捷方式:

$containerBuilder = new ContainerBuilder();
$containerBuilder->register(Localhost\Service\String\Generator::class)
    ->addArgument('15');

自PHP 5.5 + 以来,您可以使用更多防错::class符号:

$containerBuilder = new ContainerBuilder();
$containerBuilder->register('generator', Localhost\Service\String\Generator::class)
    ->addArgument('15');

现在,当类名称将被错过键入时,您的IDE将突出显示它。

答案 3 :(得分:0)

<强>除了

出于性能原因,你应该compile容器。

$container = new ContainerBuilder();
$container
    ->register('generator', 'Localhost\Service\String\Generator')
    ->addArgument('15')
;
$container->compile();