logo
hero
Štefan Hosťovecký

Web Consultant & Specialist & Full Stack Developer

Integrating TYPO3 CLI Commands in Custom Plugin's Controller Action

hero

Learn how to call TYPO3 CLI commands from within a controller action in a custom plugin using the Symfony Process component. This guide will show you how to run CLI commands and capture their output, as well as important considerations such as security.

Running TYPO3 CLI Commands from a Controller Action in a Custom Plugin

In TYPO3, the command-line interface (CLI) provides a powerful way to perform various tasks such as database updates, indexing, and more. However, sometimes you may need to run a CLI command from within a controller action in a custom plugin.

For example, you may have a custom plugin that needs to run a CLI command as part of its functionality. In this case, you can use the Symfony\Component\Process\Process class to run the command and capture the output.

Here is an example of how to call a TYPO3 CLI command from within a controller action:

<?php
declare(strict_types=1);

namespace Weyou\MyPlugin\Controller;

use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Exception\ProcessSignaledException;
use Symfony\Component\Process\Exception\LogicException;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use Symfony\Component\Process\Process;

class MyController extends ActionController {

    const PHP = '/opt/homebrew/bin/php';

    /**
     *
     * @return string
     * @throws RuntimeException
     * @throws ProcessTimedOutException
     * @throws ProcessSignaledException
     * @throws LogicException
     */
    public function myAction(): string
    {
        $args = $this->request->getArguments();
        $layout = $args['layout'] ?? self::LAYOUT;
        $url = $args['url'] ?? self::LAYOUT;

        /** @var Process $process */
        $process = new Process([self::PHP, 'vendor/bin/typo3', 'myCommand:run', $layout, $url]);
        $process->setWorkingDirectory(getcwd() . "/../");
        $process->run();
        $output = $process->getOutput();

        // do something with $output
        // echo $output;
        // exit();
    }
}

It's very important to provide path const PHP to the php binary, otherwise it will not work.

Here, myCommand:run is the command that we want to run and $layout and $url are the additional arguments passed to the command.

Please keep in mind that running commands from within a controller action can present security risks if not implemented properly. Make sure to validate any user input and properly escape any data passed to the command.

Additionally, please check the TYPO3 and PHP error logs for any error messages that may indicate why the command is not executing correctly.

In conclusion, the Symfony\Component\Process\Process class provides a powerful way to run TYPO3 CLI commands from within a controller action in a custom plugin, but it is important to consider the security implications and to validate and properly escape any user input.