Zend Framework run multiple controllers using command line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Befrulezz
Forum Newbie
Posts: 2
Joined: Wed Apr 11, 2007 3:42 am

Zend Framework run multiple controllers using command line

Post by Befrulezz »

I'm trying to build a console application using the Zend Framework. I want to run multiple controllers/actions using console commands:

Code: Select all

C:\Php5\php.exe -f index.php -- 
-controller create name Main 
-provider add type Universeel name Universeel controller Main 
-exporter add type MsSql name MsSql controller Main 
-controller run name Main
I created a plugin to handle the commands and attached it to the Frontcontroller. Altough I'm getting an error:

Code: Select all

Fatal error: Call to a member function isDispatched() on a non-object in D:\Webserver\Web\wp_import\library\Zend\Controller\Action.php on line 499
Does anybody now how I can run multiple controllers using the request object...?

This is the code of the plugin:

Code: Select all

<?php
/** HausDesign_Controller_Plugin_ConsoleCommands_Actions */
Zend_Loader::loadClass('HausDesign_Controller_Plugin_ConsoleCommands_Actions');

/**
 * @category   HausDesign
 * @package    HausDesign_Controller
 * @subpackage Plugins
 * @copyright  Copyright (c) 2005-2007 HausDesign (http://www.hausdesign.nl)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class HausDesign_Controller_Plugin_ConsoleCommands extends Zend_Controller_Plugin_Abstract
{
    /**
     * Actual Arguments
     * @var array 
     */
	private $_argv;

	public function __construct()
	{
		global $argv;

		$this->_argv = $argv;
	}

    /**
     * Called before an action is dispatched by Zend_Controller_Dispatcher.
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
	{
		try {
			$consoleCommands = Zend_Registry::get('consoleCommands');
		} catch (Exception $exNoConsoleCommandsList)
		{
			$commands = array();
			$commandIndex = -1;
			for ($i = 0 ; $i < sizeof($this->_argv) ; $i++) {
				if (substr($this->_argv[$i],0,1) == '-') {
					$commandIndex++;
					$commands[$commandIndex]['_module'] = $this->_argv[$i];
					$commands[$commandIndex]['_params'] = array();
				} else {
					$commands[$commandIndex]['_params'][] = $this->_argv[$i];
				}
			}

			$consoleCommands = new HausDesign_Controller_Plugin_ConsoleCommands_Actions();
			$arrRequest = null;
			for ($i = 0 ; $i < sizeof($commands) - 1 ; $i++) {
				$controller = substr($commands[$i]['_module'], 1);
				$actionName = $commands[$i]['_params'][0];
				$params = array();
				for ($o = 2 ; $o < sizeof($commands[$i]['_params']) ; $o = $o + 2) {
					$params[$commands[$i]['_params'][$o-1]] = $commands[$i]['_params'][$o];
				}

				$arrObject = array();
				$arrObject["controller"] = $controller;
				$arrObject["action"] = $actionName;
				$arrObject["params"] = $params;

				$consoleCommands->add($arrObject);
			}
			Zend_Registry::set('consoleCommands', $consoleCommands);
		}

		return($request);
	}

    /**
     * Called after an action is dispatched by Zend_Controller_Dispatcher.
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function postDispatch(Zend_Controller_Request_Abstract $request)
	{
		try {
			$consoleCommands = Zend_Registry::get('consoleCommands');
			try {
				$object = $consoleCommands->getNext();

				$request->setControllerName($object["controller"]);
				$request->setActionName($object["action"]);
				$request->setParams($object["params"]);
				$request->setDispatched(false);
			} catch (Exception $exGetNextCommand) {
				Zend_Debug::dump('Plugin [consoleCommands] :: Actionlist finished');
			}
		}
		catch(Exception $exGetConsoleCommands) {
			Zend_Debug::dump('Plugin [consoleCommands] :: Cannot load actionlist');
		}
	}
}
?>

Code: Select all

<?php
/**
 * @category   HausDesign
 * @package    HausDesign_Controller
 * @subpackage Plugins
 * @copyright  Copyright (c) 2005-2007 HausDesign (http://www.hausdesign.nl)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class HausDesign_Controller_Plugin_ConsoleCommands_Actions
{
	private $_objects;
	
    /**
     * Called before an action is dispatched by Zend_Controller_Dispatcher.
     *
     * @param  array $object
     * @return void
     */
 	public function add(array $object)
	{
		$this->_items[] = $object;	
	}
	
    /**
     * Called before an action is dispatched by Zend_Controller_Dispatcher.
     *
     * @return array
     */
 	public function getNext()
	{
		if (sizeof($this->_objects) > 0) {
			$this->_objects = array_reverse($this->_objects);
			$object = array_pop($this->_objects);
			$this->_objects = array_reverse($this->_objects);
			return($object);
		} else {
			throw new Exception('Action stack is empty');
		}
	}
}
?>
Befrulezz
Forum Newbie
Posts: 2
Joined: Wed Apr 11, 2007 3:42 am

Post by Befrulezz »

Does anybody now if there's a forum with more "Zend Framework Experts"?
Post Reply