Ready-made extensions and custom development for Joomla!

  • Home
  • Blog
  • Using CLI applications in Joomla

Using CLI applications in Joomla

Starting with Joomla 2.5 you have a really interesting new feature available: the chance to build CLI (Command Line Interface) application.

Let's see what is this, when you should use it and a basic CLI sample.

What is a CLI application?

As you may know, Joomla is a web application: it's built to be invoked via a web server, following a request from a browser. In time, as advanced extensions have been added to Joomla, there was the need to run some pages automatically, for example via a scheduled command. Think of a backup extension: wouldn't it be nice to run it automatically at each time every day instead of logging in manually into the backend and click on the "Start backup" button?

In earlier versions of Joomla, a few simple solutions involved using curl or wget to call a specific page, with an additional authentication key passed on as parameter. Basically you had to emulate the user clicking the button in the browser, with only some minor simplications. But later on the work took on a different route: running Joomla outside the web server. Without going too deep into the relationship between PHP and Apache (or any other web server), let's just say that the target was to run Joomla as a PHP script from the command line, by loading just the minimum amount of code needed, and without the need to worry about web stuff since nothing was going to be displayed inside a browser.

That's where the concept of a "CLI application" was born. 

The first drafts of CLI applications were a bit complex because they had to load lots of stuff anyway. Thankfully, the advent of the Joomla! Platform* in Joomla 2.5 made that easier, because now you have a bigger separation between the framework (libraries) and the CMS application and so you can use the first without running the second. That's what the JApplicationCLI class is for.

* the Joomla! Platform changed name again later on but that's another story

When should I use a CLI application?

We already mentioned a very significant use case above: an enhancement of a backup extension. Not surprisingly, the Akeeba Backup extension include a CLI app to run the backup from command line.

But there are many uses for this, most of which are related to scheduled operations for existing extensions. A few ideas:

  • e-commerce clean up: each night, delete all orders that are "pending" by more than 7 days
  • e-commerce statistics: get data from a specific table, process the data, and create a CSV or graphic output 
  • monitoring: check Joomla logs / tables to see that everything is working fine

Of course you don't need to do this with Joomla. You may write your own PHP script to query the logs or the tables, and do whatever processing you need. But if you use a Joomla CLI application you can:

  • use the same framework used when you write any extension, that you already know and use almost daily (so it's easier to write)
  • re-use the code in your extensions, especially the models to access the data (so reducing the code you have to write)

You can see that it can be a very useful addiction to your power tools.

How do I write a CLI application?

Let's see a very basic sample code.

define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));

require '../libraries/import.php';

class HelloWorld extends JApplicationCli
  public function execute()
    $this->out('Hello World');


The first three lines of code will be the same in all your CLI applications, unless you have some additional initializations commands.

Then, you have the definition of your CLI app, and most importantly the execute method, which basically tells your application what to do when invoked. P.S. if you want to write good code you won't put everything into the execute method but write other small methods to do specific tasks within the application.

Finally, the last line actually starts the application.

This sample has been taken from!_Platform, where you can find more advanced examples.


Joomla CLI apps are very easy to write and should be your first choice whenever you want to automatically run a task in your extension.