我正在尝试将命令类注册为Symfony中的服务。运行bin / console时,出现以下错误:
In App_KernelDevDebugContainer.php line 644:
Attempted to load class "Runner" from namespace "App\Command\Circuit".
Did you forget a "use" statement for another namespace?
这是我注册服务的方式:
services:
App\Command\Circuit\Runner:
tags:
- { name: 'console.command', command: 'run' }
这就是命令类的样子:
namespace App\Command\Circuit;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Runner extends Command
{
protected function configure()
{
$this
->setName('run');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$time = time();
file_put_contents("{$time}.txt", 'test');
}
}
我关注了the symfony tutorial,但是我确定我忘记了连接此服务的某些事项。
所有命令必须以命令后缀结尾。
您只需要将课程从class Runner extends Command
更改为class RunnerCommand extends Command
编辑:根据Cerad的评论,自Symfony 4起,后缀不再是必需的(在3.4中,后缀仍然是必需的,文档:https://symfony.com/doc/3.4/console.html#creating-a-command)