Running Zend Framework MVC in your plain ol’ PHP project

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

3 thoughts on “Running Zend Framework MVC in your plain ol’ PHP project

  1. Avec une composition pareille, je pense que je tenterais le lavage à la main, voire machine à basse T) et sur "délicat", sans essorage…Je n'étais pas sûre pour la robe d'Eleonore, me souvenais pas de la marque de la sienne, mais finalement si.

  2. got me to thinking about what if the population was only 10% of current numbers, might be a positive thing. So how are we going to tell the other 90% they gotta go? (LOL) As to these 2 clowns shooting? The one fella definitely needs a SERIOUS talking to about range safety, and what an idiot he is, possible punctuated with a couple of smacks upside his head.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s