[英]Symfony dependency injection and your own services
I am trying to register my own class as a services with help of symfony dependency injection component, but i have problems with class loading. 我试图借助symfony依赖项注入组件将自己的类注册为服务,但类加载遇到问题。
I have file structure as this: 我有这样的文件结构:
My Generator class is simple 我的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;
}
}
And Index is 索引是
<?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();
What i am getting is an error 我得到的是一个错误
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
Or as a picture 或作为图片
Solution is simple, i forgot to modify composer config to load my services 解决方案很简单,我忘了修改composer config来加载我的服务
"autoload": {
"psr-0": {"Localhost": "src/"}
},
$oContainer = new ContainerBuilder();
$oContainer
->register('generator', 'Localhost\Service\String\Generator')
->addArgument('15');
Edit: Since Symfony 3.3+ (May 2017) you can use register()
class name service shortcut: 编辑: 自Symfony 3.3+ (2017年5月)起,您可以使用register()
类名称服务快捷方式:
$containerBuilder = new ContainerBuilder();
$containerBuilder->register(Localhost\Service\String\Generator::class)
->addArgument('15');
Since PHP 5.5+ you can use more fail-proof ::class
notation: 从PHP 5.5+开始,您可以使用更多的防故障::class
表示法:
$containerBuilder = new ContainerBuilder();
$containerBuilder->register('generator', Localhost\Service\String\Generator::class)
->addArgument('15');
Now, when class name will be miss-typed, your IDE will highlight it. 现在,当类名输入错误时,您的IDE将突出显示它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.