We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Calling a CLI script inside a Phalcon MVC project


I have a MVC application inside which I want to include a CLI project and call a task from a controller. Is this possible ? By the way, each element works fine except when I combine both.

Folder structure :


Controller :

        use Phalcon\Mvc\Model\Criteria;
        use Phalcon\Paginator\Adapter\Model as Paginator;

        class MyController extends ControllerBase {

            public function cliAction() {

                if (filter_input(INPUT_GET, 'ORDERID', FILTER_SANITIZE_ENCODED)) {

                    $orderID = filter_input(INPUT_GET, 'ORDERID', FILTER_SANITIZE_ENCODED);
                    $subscriberID = filter_input(INPUT_GET, 'SUBSCRIBERID', FILTER_SANITIZE_ENCODED);
                    $email = filter_input(INPUT_GET, 'CUSTOMEREMAIL', FILTER_SANITIZE_EMAIL);

                    if (filter_input(INPUT_GET, 'IP', FILTER_SANITIZE_ENCODED)) {

                        $IP = filter_input(INPUT_GET, 'IP', FILTER_SANITIZE_ENCODED);
                    } else {

                        $IP = filter_input(INPUT_SERVER, 'REMOTE_ADDR');

                    exec("php app/cli.php task main $orderID $subscriberID $email $IP");


Task :

    class mytaskTask extends \Phalcon\CLI\Task {

        public function mainAction(array $param) {

            $orderID = $param[0];
            $subscriberID = $param[1];
            $email = $param[2];
            $IP = $param[3];

            $timeZone = new DateTimeZone('America/Montreal');

            $date = new DateTime();
            $transactionDate = $date->format('Y-m-d h:i:s');

            $transactionLog = new TransactionLog();
            $transactionLog->IP = $IP;
            $transactionLog->Email = $email;
            $transactionLog->OrderID = $orderID;
            $transactionLog->SubscriberID = $subscriberID;
            $transactionLog->TransactionDate = $transactionDate;


Thank you!


I dont know your exact use-case, but I would go about this differently. Is there a reason you cant put the code in another controller method? If it needs to be a console app, why not make it a console app and use a defered job system. Phalconphp has a beanstalkd interface: https://docs.phalcon.io/en/latest/reference/queue.html

You could create a defered job and run it that way. But from the looks of the code, you could just as easily put it in another controller method.


I'm pretty new to Phalcon, and I'm not sure what you're trying to accomplish, but this is the code I used to start a chat server from a php controller :

exec("nohup php /var/www/chat/server.php &"); // turn the key and walk away

Then in the CLI chat server:

set_time_limit(0); //run indefinitely

ignore_user_abort(true); // This allows you to end the calling PHP script without ending this one

This is all assuming that you don't want any output back from the process... just to start it and be on your merry way.

can u post ur solution?