Creating and Executing Commands

A command is simply a PHP class. (Note: for the purposes of this documentation, the term Command and Message are used interchangeably. Technically, message is a more generic term, and command actually has a specific meaning within the terminology of CQRS. In this documentation we will be referring to these classes as Command classes.)

Examples of Commands in the Core

The following classes are examples of commands we use in the core:

  • Concrete\Core\Cache\Command\ClearCacheCommand
  • Concrete\Core\Page\Command\ReindexPageCommand
  • Concrete\Core\File\Command\RescanFileCommand

The first command has no properties within its class: that’s because there really isn’t much to store about how you’re going to clear the cache. By contrast, the ReindexPageCommand stores the page ID of the page you’re trying to reindex, and the RescanFileCommand is instantiated with the file ID you’d like to rescan.

Executing a Command

To execute any PHP class as a command, just call the executeCommand on the core Application object. This application object is frequently available within controllers as $app.

$this->app->executeCommand(new ClearCacheCommand());

You can also always get the Application object by running the app() method:

$app = app();
$command = new ReindexPageCommand(1);
$app->executeCommand($command);