【发布时间】:2023-03-21 18:49:02
【问题描述】:
我在尝试使用 Symfony2 的控制台组件在完整的 Symfony 堆栈应用程序中创建命令时遇到问题。
如果我尝试通过 DI 传递我的服务,当我尝试运行该命令时会抛出以下错误:
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Trying to get property of non-object
如果我使用 ContainerAwareCommand 创建命令并尝试使用
$this->getContainer()->get('mtgu.api.card.list.response.data');
我明白了
[LogicException]
The container cannot be retrieved as the application instance is not yet set.
我的服务正在被加载,因为它在前端控制器中使用。这个问题变得陌生,好像我传递了一个存储库服务 - 我没有遇到这个问题!
是否有一些技巧可以让服务通过这个?还是我搞砸了我的配置?
我通过在我的 DI 扩展中执行此操作来“自动加载”我的所有服务,而不是通过主 services.yml 将它们全部包含在内。我认为这个或 yml 的排序可能会影响它 - 但我已经尝试手动包含所有内容但仍然没有乐趣!
$finder = new Finder();
$finder->name('services.yml');
/**
* @var $file SplFileInfo
*/
foreach($finder->in(__DIR__.'/../') as $file) {
$loader = new Loader\YamlFileLoader($container, new FileLocator($file->getPath()));
$loader->load('services.yml');
}
供应商/Bundle/Command/services.yml
services:
mtgu.command.slugify:
class: MightyStudios\MtguBundle\Command\SlugifyCommand
tags:
- { name: console.command }
arguments:
- @mtgu.api.card.list.response.data
我认为这可能只是一些配置问题,但谷歌让我找不到答案!有没有其他人遇到过这个问题,他们能解释一下吗!?
非常感谢
【问题讨论】: