Calling a CLI script inside a Phalcon MVC project

Hi,

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 :

MyApp
    app
        config
        controllers
            MyController.php
        models
        tasks
            MyTask.php
        views
        cli.php

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");

                    $this->view->disable();
                }
            }
        }

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();
            $date->setTimezone($timeZone);
            $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;

            $transactionLog->create();
        }
    }

Thank you!



9.9k

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: http://docs.phalconphp.com/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.



533

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:

settimelimit(0); //run indefinitely

ignoreuserabort(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.