Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phalcon and cli applications

Is it possible to use phalcon in cli applications to handle requests with argv parameters? I want to use argv parameters to understand command that should be executed, e.g. ./script.php robot/create --color=red --feature=weapon

and to get this inside my application with controllers, actions, etc in this way:

controller: robot action: create GET params: color=red,feature=weapon

Is it possible using CLI classes like

  1. Phalcon\ClI\Dispatcher http://docs.phalconphp.com/en/latest/api/Phalcon_CLI_Dispatcher.html

  2. Phalcon\CLI\Console http://docs.phalconphp.com/en/latest/api/Phalcon_CLI_Console.html

  3. Phalcon\CLI\Task http://docs.phalconphp.com/en/latest/api/Phalcon_CLI_Task.html

and other similar?

There are no docs and how-to manuals... Perhaps somebody has experience or just an idea. I understand that we have to define DI and initialize application, but how to make this in a more native way I just don't have any ideas.

Also, one more question: can phalcon handle argv parameters automatically?

As I understand, we should start Phalcon\CLI\Console object as application and pass to it DI. But the whole process/scenario... I just can't get it :)

like image 398
avasin Avatar asked Feb 11 '13 07:02

avasin


3 Answers

So, i have following folders structure:

~/www
~/www/app
~/www/app/models
~/www/app/controllers - controllers for web
~/www/app/tasks - task for cli
~/www/public/app.php - web application
~/www/cli/app.php - console application

In cli/app.php i have following code:

#!/usr/bin/php
<?php

/**
 * This makes our life easier when dealing with paths. 
 * Everything is relative to the application root now.
 */ 
chdir(dirname(__DIR__));

/**
 * Init loader
 */
$loader = new \Phalcon\Loader();
$loader->registerDirs(['app/tasks/'])
       ->register(); 

/**
 * Setup dependency injection      
 */       
$di = new Phalcon\DI();

// Router
$di->setShared('router', function() {
    return new Phalcon\CLI\Router();
});

// Dispatcher
$di->setShared('dispatcher', function() {
    return new Phalcon\CLI\Dispatcher();
});

/**
 * Run application
 */
$app = new Phalcon\CLI\Console();
$app->setDI($di);
$app->handle($argv);

then i put my tasks classes in app/tasks folder.. and it just works. perhaps this will help somebody ;)

like image 189
avasin Avatar answered Sep 22 '22 00:09

avasin


I'm a Phalcon enthusiast, but actually tasks seem to me something to create cron jobs. They are something different from commands. I don't think tasks are the right way to create a complete cli application. There are many limitation. I really suggest you to use Symfony Console instead: it's well documented, it manages multi arguments and params, it provides automatically a command line help and you can install it via Composer. If you are creating cron jobs you can go with Phalcon tasks, but if your intention is to create a cli application, take a look at Symfony Console.

like image 37
noun Avatar answered Sep 22 '22 00:09

noun


can phalcon handle argv parameters automatically?

yes, PHP will pass command line parameters to $argv in any framework.

like image 24
joy Avatar answered Sep 19 '22 00:09

joy