Au sein d’un projet Zend Framework, il peut être utile qu’un script indépendant puisse profiter des fonctionnalités de ZF, et des classes/templates qui y sont chargés.
Voici un exemple de script PHP utilisé comme tâche Cron.
Fichier BootstrapCron.php
qui sera inclue par le script :
mb_internal_encoding('utf-8');
mb_regex_encoding('utf-8');
setlocale(LC_ALL, 'fr_FR.utf8');
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/..'));
// Define path to library directory
defined('LIBRARY_PATH')
|| define('LIBRARY_PATH', realpath(dirname(__FILE__) . '/../../library'));
// Define path to public directory
defined('PUBLIC_PATH')
|| define('PUBLIC_PATH', realpath(dirname(__FILE__) . '/../../public'));
define('APPLICATION_ENV','development');
// Ensure library is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(LIBRARY_PATH),
realpath(APPLICATION_PATH),
realpath(APPLICATION_PATH . '/models'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
// custom classes
$autoloader->registerNamespace('Util_');
require_once 'Zend/Application.php';
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$application->bootstrap();
$view = new Zend_View();
$view->setScriptPath(APPLICATION_PATH . '/views/scripts/');
Le script en question :
require_once 'BootstrapCron.php';
$tableUser = new Table_User();
$view->user = $tableUser->get(1);
$content = $view->render('user/profile.phtml');
…