In my Symfony project I have created a Command class to delete specific user.
I injected required parameter "email" in the Command class constructor.
I have never tried to implement command in the Controller so I have problem there.
I want to trigger the API call in the Controller which will return desired json output if command is successful.
How can I accomplish that?
My Command class:
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!');
}
And my try in controller to get what I want:
/**
* @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;
}
And the main question is how to pass email parameter in command that is needed to be provided for this endpoint?
question from:
https://stackoverflow.com/questions/65901719/symfony-return-json-output-of-command-class-from-controller 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…