How to Add CLI in Slim 3 Framework and Schedule it Using Cron


When developing a web application, sometimes we need to schedule some task. Most of the time we will create a php file which then create cron schedule to run the file. Here in this post I will share how to add CLI in Slim 3 Framework and schedule it using cron.

Assuming there's already Slim 3 Framework installed, here are the steps:

Install symfony/console Package

Install symfony/console from packagist using composer, by issuing this command on your terminal:

$ composer -vvv require symfony/console

Create Necessary Files

Once symfony/console installed, create new php file called cli.php and put it to main directory.


require __DIR__.'/vendor/autoload.php';

use App\Command\RefreshIgLatestPost;
use Symfony\Component\Console\Application;

$command = new RefreshIgLatestPost();

$application = new Application();


Next step is create the command file. In this example, I put the file in /src/commands/RefreshIgLatestPost.php


namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class RefreshIgLatestPost extends Command
    protected function configure()
            // the name of the command (the part after "bin/console")

            // the short description shown while running "php bin/console list"
            ->setDescription('Refresh latest post on Algolia dataset.')

            // the full command description shown when running the command with
            // the "--help" option
            ->setHelp('This command allows you refresh Algolia dataset with latest post.')

    protected function execute(InputInterface $input, OutputInterface $output)

        // Example code
        $output->writeLn("Fetched records from Algolia");


Then we register the command class created above in composer.json

// ...omitted

    "autoload": {
        "psr-4": {
            "App\\Command\\": "src/commands"

// ...omitted


Executing the command

After configuring and registering the command, you can execute it in the terminal:

$ php cli.php app:refresh-ig-latest-post

Put the Command into Schedule with Cron

To run the command in a scheduled manner, we should add the command to cron. Here's an example to schedule it every 5 minutes:

$ crontab -e

*/5 * * * *  /usr/bin/php /your/php/application/directory/cli.php app:refresh-ig-latest-post >/dev/null 2>&1

Final Words

I hope that you now know how to add CLI in Slim 3 Framework and schedule it using cron. If you run into any issues or have any feedback feel free to drop a comment below.





1 comment

  1. Hi

    I found your post when looking for a way to use the Slim3 framework as a command line router.

    Your solution works, but it does not use Slim3 in any way. According to your post you are just installing Symfonys Console, which is nice, but maybe you should change the title of the post to something like "Use Symfony Console together with Slim3"

    Best regards

    • Thanks for you suggestion. As a matter of fact, the code mentioned on the post are taken from Slim 3 application. The command RefreshIgLatestPost was meant to update latest post from Instagram for specified user(s). I could just use cron to run the PHP file straight away but I want to provide a cleaner interface for my clients as they already familiar with Laravel's artisan command. That's the story behind this post.

Leave a reply

Your email address will not be published. Required fields are marked *