我在Symfony中使用PhpStorm。我想做的是使用调试按钮( 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
。
有什么想法吗?
答案 0 :(得分:0)
您遵循了什么文档?
要创建命令,您需要扩展命令,无需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太老了
答案 1 :(得分:0)
扩展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')