9951 explained code solutions for 126 technologies


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

Edit this code on GitHub