【发布时间】:2021-01-26 12:52:22
【问题描述】:
在我的 Symfony 项目中,我创建了一个命令类来删除特定用户。
我在 Command 类构造函数中注入了必需的参数“email”。
我从来没有尝试在控制器中实现命令,所以我有问题。
我想在 Controller 中触发 API 调用,如果命令成功,它将返回所需的 json 输出。
我怎样才能做到这一点?
我的命令类:
protected static $defaultName = 'user:delete';
$entityManager;
private $userService;
private $email;
public function __construct(string $email = null, EntityManagerInterface $entityManager, KeycloakApi $keycloakApi)
{
parent::__construct($email);
$this->entityManager = $entityManager;
$this->userService = $userService;
}
protected function configure()
{
$this
->setDescription('Deletion of selected user.')
->addArgument('email', InputArgument::REQUIRED, 'User email');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$user = $this->userService->getUserByEmail($this->email);
if (empty($user)) {
throw new Exception('USER_DOESNT_EXIST');
}
$this->userService->deleteUser($user['id']);
$output->writeln('Done!');
}
我尝试在控制器中得到我想要的:
/**
* @Route("/delete/test", name="delete_test")
*/
public function testDelete(): JsonResponse
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
$input = new ArrayInput(array("user:delete"));
$output = new BufferedOutput();
// Run the command
$retval = $application->run($input, $output);
dump($retval);die;
}
主要问题是如何在需要为此端点提供的命令中传递电子邮件参数?
【问题讨论】:
-
你为什么不直接从你的控制器调用
userService?这是一种避免在控制器中调用命令的做法,因为您正在混合使用 cli 和 web contecxt。它也损害了表演而没有收获 -
会有三种删除方式,这是第一种。所以一个 api 调用将同时处理所有三个。所有三个都将输入到命令类中。 @麦斯基
-
这是一个非常糟糕的主意。如果您想隔离逻辑以供重用,请将其移至 HTTP 控制器和控制台命令都使用的另一层。
标签: php api symfony command symfony4