Skip to content

Instantly share code, notes, and snippets.

@papayasoft
Created May 29, 2012 04:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save papayasoft/2822456 to your computer and use it in GitHub Desktop.
Save papayasoft/2822456 to your computer and use it in GitHub Desktop.
Zend Framework bootstrap without Zend_Application
<?php
// A first run at a Zend Framework pre-1.8 bootstrap file that does not use Zend_Application
// Not tested, just thinking out loud...
// Do your PHP settings like timezone, error reporting
// ..
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/_zf/application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
// Get autoloading in place
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
// Any additional configs to autoloader, like custom autoloaders
// Read config
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
// bootstrap resources manually:
// * create db adapter
// * create resource autoloaders with the mappings you need
// * etc
// Get the singleton front controller
$front = Zend_Controller_Front::getInstance();
// Set controller directory
$front->setControllerDirectory(APPLICATION_PATH . '/controllers');
// Or set module directory
$front->setModuleDirectory(APPLICATION_PATH . '/modules');
// Other front config, like throw exceptions, etc.
// ...
//
// Create a router
$router = new Zend_Controller_Router_Rewrite();
// Add routes to the router
$router->addRoute('myRoute', new Zend_Controller_Router_Route(array(
// your routing params
)));
// More routes...
// Alternatively, the routes can all be in an xml or ini file and you can add
// them all at once.
// Tell front to use our configured router
$front->setRouter($router);
// Add an plugins to your $front
$front->registerPlugin(new My_Plugin());
// other plugins...
// Dispatch the request
$front->dispatch();
// Notes:
//
// There might be some View/ViewRenderer stuff to do, as well. But as noted in
// other places, the ViewRenderer incurs a non-trivial performance hit. If
// performance is the issue, then you'll want to disable the ViewRenderer and
// make your action controllers call their own rendering using
// $this->view->render('my/view-script.phtml')
// When you call $front->dispatch(), the $request and $response objects will be
// created automatically. If you want to do something specific to them
// at bootstrap - like setting the charset in Content-Type header of the
// response - then you can create your request/response object yourself, do
// what you want to it, and then attach it to the
// front with $front->setResponse($response);
// Same for the request object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment