【发布时间】:2019-01-21 09:24:13
【问题描述】:
我添加了 Symfony 依赖注入容器 (composer require symfony/dependency-injection):https://symfony.com/doc/current/components/dependency_injection.html
运行:composer dump-autoload -o 重新组织类(通过 composer 安装新库后,/www/vendor/composer/autoload_classmap.php 文件为空)。
在我的init.php 文件中运行它:
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use MyApp\Core\App;
use MyApp\Core\Database;
use MyApp\Models\SystemUser;
use MyApp\Models\Customer;
use MyApp\Core\Log;
$container = new ContainerBuilder();
$container->autowire( Log::class );
$container->autowire( Database::class );
$container->autowire( SystemUser::class );
$container->autowire( Customer::class );
$container->autowire( App::class )
->setPublic( true );
$container->compile();
$app = $container->get( App::class );
在compile() 上休息。
我错过了什么?
找不到任何线索来解决此问题。
我做错了什么?
PHP Fatal error: Uncaught Error: Class 'Symfony\Component\Config\Resource\ClassExistenceResource' not found in /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php:385
Stack trace:
#0 /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php(225): Symfony\Component\DependencyInjection\Compiler\AutowirePass->createTypeNotFoundMessage(Object(Symfony\Component\DependencyInjection\TypedReference), 'argument "$db" ...')
#1 /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php(256): Symfony\Component\DependencyInjection\Compiler\AutowirePass->Symfony\Component\DependencyInjection\Compiler\{closure}()
#2 /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php(165): Symfony\Component\DependencyInjection\Compiler\AutowirePass->autowireMethod(Object(ReflectionMethod), Array)
#3 /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php(123): Symfony\Component\DependencyInjection\Compiler\AutowirePass->autowireCalls(Object(ReflectionClass), true)
#4 /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php(71): Symfony\Component\DependencyInjection\Compiler\AutowirePass->doProcessValue(Object(Symfony\Component\DependencyInjection\Definition), true)
#5 /www/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php(82): Symfony\Component\DependencyInjection\Compiler\AutowirePass->processValue(Object(Symfony\Component\DependencyInjection\Definition), true)
#6 /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php(100): Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Array, true)
#7 /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php(71): Symfony\Component\DependencyInjection\Compiler\AutowirePass->doProcessValue(Array, true)
#8 /www/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php(46): Symfony\Component\DependencyInjection\Compiler\AutowirePass->processValue(Array, true)
#9 /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php(52): Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->process(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#10 /www/vendor/symfony/dependency-injection/Compiler/Compiler.php(95): Symfony\Component\DependencyInjection\Compiler\AutowirePass->process(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#11 /www/vendor/symfony/dependency-injection/ContainerBuilder.php(750): Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#12 /www/myapp/init.php(50): Symfony\Component\DependencyInjection\ContainerBuilder->compile()
#13 /www/public/index.php(10): require_once('/www/myapp/...')
#14 {main}
thrown in /www/vendor/symfony/dependency-injection/Compiler/AutowirePass.php on line 385
composer.json
{
"name": "potato/www",
"authors": [
{
"name": "potato potato",
"email": "potato@MyApp.com"
}
],
"require": {
"monolog/monolog": "^1.24",
"filp/whoops": "^2.3",
"symfony/dependency-injection": "^4.2"
},
"autoload":{
"psr-4": {
"MyApp\\": "myapp"
}
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.3"
}
}
【问题讨论】:
-
我不明白你为什么要这样做,如果你不使用整个 symfonmy 框架,为什么还要使用“symfony/dependency-injection”
-
因为我想使用 Symfonys DIC,而不是 Pimple/PHP DI/DICE/ect'。这有多糟糕? @Zeljka
-
我想我之前提到过这一点,但如果您的目标是交付一个可以工作的应用程序,那么请使用完整的框架(symfony 或 laravel),并附带所有出色的文档和社区支持.一旦您掌握了几年,您将积累必要的知识来自己连接组件,以便在缺少默认设置时更好地解决您的问题。目前你根本没有理解水平来做到这一点。
-
连接第三部分组件的好处是您可以定义自己的架构。定义自己的架构很困难,并且需要您没有的经验。这就是框架的主要好处之一——为您解决架构问题。
-
如果你只是想更深入地了解它们是如何组合在一起的,这样你在使用框架时不会感到迷茫,那么请遵循 symfony 提供的这个相当不错的教程:symfony.com/doc/current/create_framework/index.html 当你完成后,扔掉你的代码并在适当的框架中构建你的项目,这样你就可以访问你需要的社区和文档
标签: php symfony dependency-injection symfony-dependency-injection