As PHP developers, it is convenient to be able to write command line scripts in PHP. In doing so, you will almost certainly want access to Zend Framework components and their configurations as if you are writing a normal MVC app, but without invoking the MVC stack and without loading unnecessary resources. I’ve seen solutions where actions are exposed as controller actions and called by wget – these are counter-intuitive, inefficient, and will suffer from max execution timeouts and other problems.
Zend_Applicaton to the rescue
Lets start with our website’s public/index.php – the script which sets up and launches an application. It looks roughly like this:
<?php // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); require_once 'Zend/Application.php'; $application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/configs/config.php' ); $application->bootstrap()->run();
One feature of Zend Application is that you can choose to bootstrap only certain resources, rather than all of those referenced in your application config and bootstrap (_init methods.) The following assumes that our script needs to use the database, and send an email.
<?php // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); require_once 'Zend/Application.php'; $application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/configs/config.php' ); //only load resources we need for script, in this case db and mail $application->bootstrap(array('db', 'mail'));
Having bootstrapped the resources, we can retrieve them from the application as follows:
$db = $application->getBootstrap()->getResource('db'); //do something! $row = $db->fetchRow('SELECT * FROM something');
Tags: cli, php, zend-application, zend-framework
[...] Caunt has posted an article on how to access your Zend_Application resources from a command line application: As PHP developers, it is convenient to be able to write command line scripts in PHP. In doing so, [...]
[...] to David Caunt for this very useful [...]
Hm… I expected some Zend_Console_Getopt example…
Hi Alexander,
I’m planning to extend this guide to include a Zend_Console_Getopt example. It should be up in a day or two.
David
David, thx!
It’s very simple and graceful solution!
[...] Caunt has posted an article on how to access your Zend_Application resources from a command line application: As PHP developers, it is convenient to be able to write command line scripts in PHP. In doing so, [...]
[...] http://www.davidcaunt.co.uk/2010/02/25/easy-command-line-scripts-with-zend-application/ [...]
It’s been a few days did you forget about the Zend_Console_Getopt example?
Reminder: I’m following your great post but I would also like to read about Zend_Console_Getopt
Hi there,
How can i get APPLICATION_ENV in a command line batch process ?
echo getenv(‘APPLICATION_ENV’); return FALSE
Best regards
David
Hi David,
In the case of command line processes, getenv retrieves variables from the shell. You can set a variable in your bash initialisation file as follows:
export APPLICATION_ENV=developmentThe location of this file varies but in OS X you can use ~/.bash_profile and on Linux look for ~/.bashrc or similar. On Windows it is possible to set environment variables by pressing WIN + BREAK and the clicking the relevant link.
[...] is critical to keep the purpose of the website you’re building at the forefront of your mind. Close early and often. Website builder builds the website based on templates. It may seem like an easy-to-answer question [...]