我正在使用PhpStorm和Symfony。我想要做的是使用调试按钮(Shift + F9)从IDE内部调试Symfony命令。
我收到以下错误。
PHP致命错误:第8行上的/home/user/Projects/project1/symfony/src/AppBundle/Command/testScriptCommand.php中找不到类'Symfony \ Bundle \ FrameworkBundle \ Command \ ContainerAwareCommand'PHP堆栈跟踪:
这很奇怪,因为我已经按照Symfony文档创建命令,我已经包含了以下类:
<?
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class testScriptCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('app:test-script');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
echo 1;
}
}
调试器在IDE内工作直到第8行,并且一旦尝试继续它就会失败,并且已经提到了致命错误。
在我看来,第4行实际上并没有导入所需的ContainerAwareCommand
。
有任何想法吗?
您遵循了哪些文件?
要创建命令,您需要扩展Command,而不是ContainerAwareCommand
// src/Command/CreateUserCommand.php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CreateUserCommand extends Command
{
// the name of the command (the part after "bin/console")
protected static $defaultName = 'app:create-user';
protected function configure()
{
// ...
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// ...
}
}
有关更多信息:https://symfony.com/doc/current/console.html
编辑:
添加信息......
ContainerAwareCommand用于Symfony版本<= 2.6 https://symfony.com/doc/2.6/cookbook/console/console_command.html Soooo old
扩展Symfony\Component\Console\Command\Command
依赖关系使用命令构造函数注入ContainerInterface,就像这样 - 在我的例子中使用自动服务:
/** @var ContainerInterface $container */
protected $container;
public function __construct(ContainerInterface $container)
{
parent::__construct();
$this->container = $container;
}
然后你应该可以打电话给fe。 $this->container->getParameter('project.parameter')