简体   繁体   中英

Symfony ContainerAwareCommand not found using Xdebug and PhpStorm

I am using PhpStorm with Symfony. What I am trying to do is to debug a Symfony command from inside the IDE by using the debug button ( Shift + F9 ).

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:

It's weird as I have followed the Symfony documentation for creating commands and I have included the following classes:

<?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.

It seems to me as line 4 is not actually importing the ContainerAwareCommand that is needed.

Any ideas?

What documentation did you followed?

For creating Commands you need to extend Command, no 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

EDIT:

Adding info...

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

Extend Symfony\\Component\\Console\\Command\\Command

Dependency Inject the ContainerInterface with your commands constructor, something like this - in my case using autowired services:

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

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

Then you should be able to call fe. $this->container->getParameter('project.parameter')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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