TYPO3 CMS shell scripts (CLI mode)

Besides the backend, it is also possible to run some TYPO3 CMS scripts from the command line. This makes it possible - for example - to set up cronjobs. There are two ways to register CLI scripts:

  • using the TYPO3 command-line dispatcher based on Symfony Commands.
  • creating an Extbase command controller.

The command-line dispatcher

TYPO3 uses Symfony commands to provide an easy to use, well-documented API for writing CLI commands.

Creating a new Symfony command in your extension

Symfony commands should extend the class Symfony\Component\Console\Command\Command.

TYPO3 looks in a file Commands.php in the Configuration folder of extensions for configured commands. The Commands.php file returns a simple array with the command name and class.

For example to add a command which can be called via bin/typo3 yourext:dothings add the following:

return [
    'yourext:dothings' => [
        'class' => \Vendor\Extension\Command\DoThingsCommand::class
    ],
];

The command should implement at least a configure and an execute method.

configure as the name would suggest allows to configure the command. Via configure a description or a help text can be added, or mandatory and optional arguments and parameters defined.

A simple example can be found in the ListSysLogCommand:

/**
 * Configure the command by defining the name, options and arguments
 */
protected function configure()
{
    $this->setDescription('Show entries from the sys_log database table of the last 24 hours.');
    $this->setHelp('Prints a list of recent sys_log entries.' . LF . 'If you want to get more detailed information, use the --verbose option.');
}

The execute method contains the logic you want to execute when executing the command.

A detailed description and an example can be found at the Symfony Command Documentation.

Extbase command controllers

Warning

Extbase command controllers are deprecated since TYPO3 v9. Use symfony commands as outlined above.

Note

If you do not need Extbase in your command it is recommended to directly use a Symfony command (see above).

First of all, the command controller must be registered in an extension's ext_localconf.php file (example taken from the "lang" system extension):

// Register language update command controller
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = \TYPO3\CMS\Lang\Command\LanguageCommandController::class;

The class itself must extend the \TYPO3\CMS\Extbase\Mvc\Controller\CommandController class. Each action that should be available from the command line must be named following the pattern "[action name]Command". The PHPdoc information is directly used as help text (description of the action, what arguments it takes).

Here's an extract from the command controller class of the "lang" extension:

/**
 * Language command controller updates translation packages
 */
class LanguageCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController
{
    // ...

    /**
     * Update language file for each extension
     *
     * @param string $localesToUpdate Comma separated list of locales that needs to be updated
     * @return void
     */
    public function updateCommand($localesToUpdate = '')
    {
        // ...
    }
}

This command would be called by using:

$ /path/to/php bin/typo3 extbase language:update fr

which would update translation packages for the French language.