Symfony Console Commands (cli)

It is possible to run some TYPO3 CMS scripts from the command line. This makes it possible - for example - to set up cronjobs.

TYPO3 uses Symfony commands to provide an easy to use, well-documented API for writing CLI (command line interface) commands.

These commands can also be run from the TYPO3 scheduler.

バージョン 8 で追加: TYPO3 supports Symfony Console commands natively since TYPO3 v8.

Extbase Command Controllers are deprecated since v9.4.

Creating a new Symfony Command in Your Extension

  1. Add Configuration/Commands.php to your extension

    TYPO3 looks in this file for configured commands. It should return a simple array with the command name and class.

    For example to add a command named yourext:dothings:

    return [
        'yourext:dothings' => [
            'class' => \Vendor\Extension\Command\DoThingsCommand::class,
        ],
    ];
    
  2. Create the corresponding class file: Classes/Command/DoThingsCommand.php

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

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

    configure()

    As the name would suggest allows to configure the command. Allows to add a description or a help text, or mandatory and optional arguments and parameters defined.

    execute()

    Contains the logic when executing the command.

参考

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

Command Class

Example taken from ListSysLogCommand in the core and simplified:

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class DoThingsCommand extends Command
{
    /**
     * 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.');
    }

    /**
     * Executes the command for showing sys_log entries
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);
        $io->title($this->getDescription());

        // ...
        $io->writeln('Write something');
    }
}

Passing Arguments

\TYPO3\CMS\Install\Command\UpgradeWizardRunCommand:

/**
 * Configure the command by defining the name, options and arguments
 */
protected function configure()
{
    $this->setDescription('Run upgrade wizard. Without arguments all available wizards will be run.')
        ->addArgument(
            'wizardName',
            InputArgument::OPTIONAL
        )->setHelp(
            'This command allows running upgrade wizards on CLI. To run a single wizard add the ' .
            'identifier of the wizard as argument. The identifier of the wizard is the name it is ' .
            'registered with in ext_localconf.'
        );
}

This command takes one optional argument wizardName, which can be passed on the command line:

vendor/bin/typo3 upgrade:run [wizardName]

Deactivating the Command in Scheduler

バージョン 9.4 で追加: t3core:Changelog/9.4/Feature-85991-ExcludeSymfonyCommandsFromScheduler

By default, the command can be used in the scheduler too. You can deactivate this by setting schedulable to false in Configuration/Commands.php:

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

Initialize Backend User

If anything related to DataHandler and backend permission handling is necessary, you should call this initialization method once in your execute() function:

Bootstrap::initializeBackendAuthentication();

Running the Command From the Command Line

The above example can be run via command line:

vendor/bin/typo3 yourext:dothings

Show help for the command:

vendor/bin/typo3 help yourext:dothings

ちなみに

If you installed TYPO3 without Composer, the path for the executable is typo3/sysext/core/bin/typo3.

Running the Command From the Scheduler

バージョン 9.0 で追加: Running Symfony Console Commands via the scheduler is possible since TYPO3 v9.0.

The Deactivating the Command in Scheduler option is available since v9.4.

By default, it is possible to run the command from the TYPO3 scheduler as well. In order to deactivate this, see Deactivating the Command in Scheduler.