Federico Cargnelutti

Simple is better than complex. Complex is better than complicated. | @fedecarg

Executing external tasks in the Zend Framework

leave a comment »

Duane O’Brien wrote a series of articles about three widely used PHP frameworks: Zend, Symfony, and CakePHP. He examines their similarities and differences while building and extending a sample application in each of the three frameworks.

In Part 5 he explains how to execute external tasks in the Zend Framework:

“Due to the pick-and-choose nature of the Zend Framework, creating an automated task using the framework allows for some flexibility in deciding what you use and don’t use. Because you are not going to be accessing the Web application through the existing front controller, which services Web requests, you should create an additional controller to control script execution. This controller will look similar to the front controller, in that you will be registering the autoloader and defining the base adapter for the model. But an important difference may be in your PHP installation or configuration.”

The only thing I can add to that is: If you setup a job that executes a script that accepts parameter passed via HTTP, you might need to pass them through the command line as well, for example:

php /column/protected/zend/scripts/prune.php modified=2008-02-28 archive=true

For that to work, you’ll need to detect the SAPI type first and then process the arguments.

$sapiType = substr(php_sapi_name(), 0, 3);
$params = ($sapiType == 'cli') ? $argv : $_GET;

if ($sapiType === 'cli') {
	array_shift($params);
	$args = array();
	foreach ($params as $param) {
		$param = explode('=', $param);
		if (isset($param[0]) && isset($param[1])) {
			$args[$param[0]] = $param[1];
		}
	}
	$params = $args;
}

Read Duane’s articles:

Part 1: Getting started with three popular frameworks
Part 5: Integrating external tasks

Written by Federico

February 28, 2008 at 11:28 am

Posted in Frameworks, PHP, Programming

Leave a Reply