It’s nice being able to use some of the features of Zend Framework in your old plain PHP projects. I’ve been doing this for a while, using a zend_init.php file which is almost like the index.php in the ZF public folder:

<?php
// Define path to application directory
defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application/application'));

if($_SERVER['SERVER_ADDR'] == '91.xxx.242.74')
{
 $app_env = 'production';
}
else
{
 $app_env = 'development';
}

// Define application environment
defined('APPLICATION_ENV')
 || define('APPLICATION_ENV', $app_env);
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
 realpath(APPLICATION_PATH . '/../library'),
 get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
 APPLICATION_ENV,
 APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();

That allows you to use all sorts of things like new Zend_Pdf() etc.
However I was doing an AJAX post from our plain old PHP site and I wanted it to actually run the MVC stuff!

In order to run controller actions from a plain script I did this:

<?php
require_once('../inc/zend_init.php');
$fc = Zend_Controller_Front::getInstance();
$request = new Zend_Controller_Request_Http();
$request->setRequestUri('branches/cms-save-branch-update');
$request->setParams(array(
 'module' => 'default',
 'controller' => 'branches',
 'action' => 'cms-save-branch-update'
));
$fc->setRequest($request);
$fc->dispatch($request);

You now have the full power of Zend Framework at your disposal! Have a nice day!

Advertisements