Monday, January 18, 2010

Workflow of the Model-View-Controller


In this example, we illustrate the workflow of an MVC application. We trace the controller actions. We also trace dispatch methods executed before and after a helper or a plugin.

Components used in this example
Select file | Try it | Download source code
Entry point
  • We define the path to the application directory.
  • We define the application environment.
  • We set the ZF version.
  • We add the Zend Framework and specific resources to the include path.
  • We define the application pathname for the cookie.
  • We instantiate the application and we run the application.

// We define the path to the application directory.
defined('APPLICATION_PATH') or
define('APPLICATION_PATH'realpath(dirname(__FILE__) . '/../application'));

// We define the application environment.
defined('APPLICATION_ENV') or
define('APPLICATION_ENV'
    (
getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// We set the ZF version.
$zfVersion '1.9.7'// tested with this version
$zfDir '../../../ZendFramework';
isset(
$_COOKIE['mvc-zf-version']) and is_dir("$zfDir/{$_COOKIE['mvc-zf-version']}") and
$zfVersion $_COOKIE['mvc-zf-version'];

// We add the Zend Framework and specific resources to the include path.
set_include_path(implode(PATH_SEPARATOR, array(
    
realpath(APPLICATION_PATH '/../library'),
    
realpath("$zfDir/$zfVersion/library"),
    
get_include_path(),
)));

// We define the application pathname for the cookie.
$path str_replace('\\''/'realpath(dirname(__FILE__)));
preg_match('~www(/.+?/)public~'$path$match) or die('Cannot set COOKIE_PATH!');
define('COOKIE_PATH'$match[1]);

require_once 
'Zend/Application.php';

// We instantiate the application and we run the application.
$application = new Zend_Application(
    
APPLICATION_ENV,
    
APPLICATION_PATH '/configs/application.ini'
);
$application->bootstrap()->run();

1 comment: