We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon\Cli

Where I can find example of using \Phalcon\Cli classes? It's looks very interesting.

Hi SliceOfLife, i think you can have a look at the dev tools github page :

https://github.com/phalcon/phalcon-devtools?source=cc

The' phalcon.php' file is the entry point of the application while all sources will be located under the 'scripts' directory You can also find the command used by the application under : 'scripts/Phalcon/Commands/Builtin'

Nice example, but I can't understand how to run a custom Task.

I wrote a console executable file:


#!/usr/bin/env php
<?php

error_reporting(E_ALL);

try {
    $config = require __DIR__ . '/../app/config/config.php';

    $loader = new \Phalcon\Loader();
    $loader->registerDirs([
        $config->application->tasksDir,
        $config->application->pluginsDir,
        $config->application->libraryDir,
        $config->application->modelsDir,
    ])->register();

    /**
     * @var \Phalcon\DiInterface $di
     */
    $di = require __DIR__ . '/../app/config/cli/di.php';
    $di->set('config', $config);

    $console = new \Phalcon\CLI\Console();
    $console->setDI($di);

    $console->handle($_SERVER['argv']);
} catch (\Phalcon\Exception $e) {
    echo $e->getMessage() . "\n";
} catch (\PDOException $e){
    echo $e->getMessage() . "\n";
}

If i run it from console without any parameters, the MainTask class and mainAction() exec. But if I run it with parameter "sessions", the mainAction of SessionsTask does not run (MainTask is executed again):

./console sessions

The SessionsTask.php and MainTask.php placed in one directory.

This is code of MainTask.php

class MainTask extends \Phalcon\CLI\Task
{

    public function mainAction()
    {
        echo "console tool\n";

        $this->listAction();
    }

    public function listAction() {
        echo "\n Hello " . "\n\n";
    }
}

Also, the command "./console main list" also does not work.

Thank you!