我开始写一个命令,按照文档,这就是现在的命令(非常非常简单)
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';
是您的命令名称。
- 调用:
php bin/console 设置信息
- 或更改命令名称
protected static $defaultName = 'app:create-user';
关于php - 当我尝试执行 symfony 命令时,未定义命令 "set-info",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72415048/