I would like to run a Zend Framework action to generate some files, from command line. Is this possible and how much change would I need to make to my existing Web project that is using ZF?
Thanks!
I would like to run a Zend Framework action to generate some files, from command line. Is this possible and how much change would I need to make to my existing Web project that is using ZF?
Thanks!
Just saw this one get tagged in my CP. If you stumbled onto this post and are using ZF2, it's gotten MUCH easier. Just edit your module.config.php's routes like so:
Using the config above, you would define doCliAction in your IndexController.php under your Application module. Running it is cake, from the command line:
php index.php do cli
Done! Way smoother.
UPDATE
You can have all this code adapted for ZF 1.12 from https://github.com/akond/zf-cli if you like.
While the solution #1 is ok, sometimes you want something more elaborate. Especially if you are expecting to have more than just one CLI script. If you allow me, I would propose another solution.
First of all, have in your Bootstrap.php
This method will deprive dispatching control from default router in favour of our own router Application_Router_Cli.
Incidentally, if you have defined your own routes in _initRoutes for your web interface, you would probably want to neutralize them when in command-line mode.
Class Application_Router_Cli (I assume you have autoload switched on for Application prefix) may look like:
Now you can simply run your application by executing
In this case cliAction method in BackupController controller will be called.
You can even go ahead and modify Application_Router_Cli class so that not "cli" action is taken every time, but something that user have chosen through an additional parameter.
And one last thing. Define custom error handler for command-line interface so you won't be seeing any html code on your screen
In Bootstrap.php
In ErrorController.php
I have used wget command
wget http://example.com/module/controller/action -O /dev/null
-O /dev/null
if you dont want to save the outputIt's actually much easier than you might think. The bootstrap/application components and your existing configs can be reused with CLI scripts, while avoiding the MVC stack and unnecessary weight that is invoked in a HTTP request. This is one advantage to not using wget.
Start your script as your would your public index.php:
You can then proceed to use ZF resources just as you would in an MVC application:
If you wish to add configurable arguments to your CLI script, take a look at Zend_Console_Getopt
If you find that you have common code that you also call in MVC applications, look at wrapping it up in an object and calling that object's methods from both the MVC and the command line applications. This is general good practice.
One option is that you could fudge it by doing a wget on the URL that you use to invoke the desirable action
akond idea works great, except the error exception isnt rendered by the error controller.
and In Application_Router_Cli, comment off the echo and die statement