php-symfonyHow to set up a cron job in PHP Symfony?
Setting up a cron job in PHP Symfony is easy.
First, create a command class in the src/Command
directory.
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCronJobCommand extends Command
{
protected static $defaultName = 'app:my-cron-job';
protected function configure()
{
$this
->setDescription('My cron job description.')
->setHelp('This command allows you to create a cron job...');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Do something...
}
}
Then, add the command to the config/services.yaml
file:
services:
App\Command\MyCronJobCommand:
tags:
- { name: console.command }
Finally, add the cron job to the crontab file:
* * * * * cd /path-to-your-project && php bin/console app:my-cron-job
This will execute the command every minute.
Helpful links
More of Php Symfony
- How to create a model in PHP Symfony?
- How to install Symfony on Windows?
- How to do testing with PHP Symfony?
- How to upload a file in PHP Symfony?
- How to create a migration in PHP Symfony?
- How to create a backend with PHP Symfony?
- How to get request parameters in PHP Symfony?
- How to use the PHP Symfony Finder?
- How to use websockets in PHP Symfony?
- How to check PHP Symfony version?
See more codes...