php - 当我尝试执行 symfony 命令时,未定义命令 "set-info"

标签 php symfony symfony6

我开始写一个命令,按照文档,这就是现在的命令(非常非常简单)

namespace App\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

// the name of the command is what users type after "php bin/console"
#[AsCommand(name: 'set-info')]
class CreateUserCommand extends Command
{
    protected static $defaultName = 'set-info';

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // ... put here the code to create the user

        // this method must return an integer number with the "exit status code"
        // of the command. You can also use these constants to make code more readable

        // return this if there was no problem running the command
        // (it's equivalent to returning int(0))
        echo "Hello";
        return Command::SUCCESS;

        // or return this if some error happened during the execution
        // (it's equivalent to returning int(1))
        // return Command::FAILURE;

        // or return this to indicate incorrect command usage; e.g. invalid options
        // or missing arguments (it's equivalent to returning int(2))
        // return Command::INVALID
    }
}

但是当我尝试执行它时 php bin/console app:set-info 我得到了 [critical] Error thrown while running command "set-info"。消息:“未定义命令“set-info”。”

services.yml 上的自动配置为真

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

我错过了什么吗?

最佳答案

你调用了错误的命令。

protected static $defaultName = 'set-info'; 是您的命令名称。

  1. 调用:

php bin/console 设置信息

  1. 或更改命令名称

protected static $defaultName = 'app:create-user';

关于php - 当我尝试执行 symfony 命令时,未定义命令 "set-info",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72415048/

相关文章:

用于创建带有每个面包屑链接的面包屑的Javascript?

php - 在 Laravel 的不同上下文中使用 "use"关键字背后的概念是什么?

php - 教义迁移问题

php - Symfony 6.3 迁移导致无状态验证器强制请求无状态的问题

php - 如何使用 Symfony 的序列化器正确填充 DTO 的 ArrayCollection 属性?

php - 如何检查用户输入的 IP 地址是否有效?

php - MySQL 和 HTML : Ordering a row based on a specific value

php - Doctrine 2 无法运行 SELECT 查询问题

json - oracle中如何从存储过程中获取数据

php - Symfony 6 中的命名路由,内部目录中有 Controller