首页 > 解决方案 > 在 symfony 5 中创建命令作为服务

问题描述

我正在尝试将命令类注册为 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');
    }
}   

我按照symfony 教程进行操作,但我确定有些东西我忘了连接这个服务。

标签: phpsymfonyservicecommand

解决方案


推荐阅读