How to create cron in symfony?
Here, I will show you how to create cron in symfony.
Step 1:
Navigate to Project Directory.
Step 2:
create a Cron Task using this command.
php bin/console make:command app:[cron-name]
and it will generate a [cron-name]Command.php
.
this file have two methods : configure()
and execute()
.
Step 3 – Configuration:
in configure method, there are some configuration in this method like description, argument, and options.
$this->setDescription('Add a short description for your command')
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
;
Step 4 – Execution :
In execute method have some code that will be execuated.
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$arg1 = $input->getArgument('arg1');
if ($arg1) {
$io->note(sprintf('You passed an argument: %s', $arg1));
}
if ($input->getOption('option1')) {
// ...
}
$io->success('You have a new command! Now make it your own! Pass --help to see your options.');
return Command::SUCCESS;
}
Step 5 – Run Cron :
Now Open a command-line and run this command
`php bin/console app:[cron-name]`
now its have an output.
Thanks for reading.