简体   繁体   English

使用 Xdebug 和 PhpStorm 找不到 Symfony ContainerAwareCommand

[英]Symfony ContainerAwareCommand not found using Xdebug and PhpStorm

I am using PhpStorm with Symfony.我在 Symfony 中使用 PhpStorm。 What I am trying to do is to debug a Symfony command from inside the IDE by using the debug button ( Shift + F9 ).我想要做的是使用调试按钮( Shift + F9 )从 IDE 内部调试 Symfony 命令。

I am getting the following error.我收到以下错误。

PHP Fatal error: Class 'Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand' not found in /home/user/Projects/project1/symfony/src/AppBundle/Command/testScriptCommand.php on line 8 PHP Stack trace: PHP 致命错误:在第 8 行 PHP 堆栈跟踪的 /home/user/Projects/project1/symfony/src/AppBundle/Command/testScriptCommand.php 中找不到 Class 'Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand':

It's weird as I have followed the Symfony documentation for creating commands and I have included the following classes:这很奇怪,因为我遵循了 Symfony 文档来创建命令,并且我包含了以下类:

<?php

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(): void
    {
        $this->setName('app:test-script');
    }

    protected function execute(InputInterface $input, OutputInterface $output): void
    {
        echo 1;
    }
}

The debugger works inside the IDE until line 8 and once try to continue it fails with the already mentioned fatal error.调试器在 IDE 内工作,直到第 8 行,一旦尝试继续,它就会因已经提到的致命错误而失败。

It seems to me as line 4 is not actually importing the ContainerAwareCommand that is needed.在我看来,第 4 行实际上并未导入所需的ContainerAwareCommand

Any ideas?有任何想法吗?

What documentation did you followed?你遵循了哪些文件?

For creating Commands you need to extend Command, no ContainerAwareCommand要创建命令,您需要扩展命令,没有 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)
    {
        // ...
    }
}

For more information: https://symfony.com/doc/current/console.html更多信息: https : //symfony.com/doc/current/console.html

EDIT:编辑:

Adding info...正在添加信息...

ContainerAwareCommand is for Symfony version <= 2.6 https://symfony.com/doc/2.6/cookbook/console/console_command.html Soooo old ContainerAwareCommand 适用于 Symfony 版本 <= 2.6 https://symfony.com/doc/2.6/cookbook/console/console_command.html Soooo old

Extend Symfony\\Component\\Console\\Command\\Command扩展Symfony\\Component\\Console\\Command\\Command

Dependency Inject the ContainerInterface with your commands constructor, something like this - in my case using autowired services: Dependency Inject the ContainerInterface with your commands constructor,像这样 - 在我的情况下使用自动装配服务:

    /** @var ContainerInterface $container */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        parent::__construct();
        $this->container = $container;
    }

Then you should be able to call fe.然后你应该可以调用fe。 $this->container->getParameter('project.parameter')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM