Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Pages: 1
Bonjour,
je suis actuellement en Framework version 1.6. Je souhaite passer d'un bootstrap "classique" à l'Initializer. Le soucis est que la méthode match() d'un Zend_Controller_Router_Route n'est plus appelé. Seul le constructeur est appelé.
bootstrap.php
error_reporting(E_ALL|E_STRICT); ini_set('display_startup_errors', 1); ini_set('display_errors', 1); // Configuration de la timezone date_default_timezone_set('Europe/Paris'); // Chemin d'accès local aux fichiers define('ROOT', realpath(dirname(__FILE__) . '/../')); set_include_path(ROOT . '/application' . PATH_SEPARATOR . ROOT . '/library' . PATH_SEPARATOR . ROOT . '/application/modules/module1/models' . PATH_SEPARATOR . get_include_path()); // Bootstrap require_once ('My/Loader.php'); Zend_Loader::registerAutoload('My_Loader'); // Chargement de la configuration $config = new Zend_Config_Ini('config.ini', 'test'); $registry = Zend_Registry::getInstance(); $registry->set('config', $config); // Mise en place de la base de donnée if (isset($config->db)) { $db = Zend_Db::factory($config->db); Zend_Db_Table::setDefaultAdapter($db); try { $db->getConnection(); } catch(Zend_Db_Adapter_Exception $e) { echo $e->getMessage(); } } // Mise en place du contrôleur $frontController = Zend_Controller_Front::getInstance(); $frontController->throwExceptions(true); // Aides d'action Zend_Controller_Action_HelperBroker::addPath(ROOT . '/application/modules/module1/helpers', 'Zend_Controller_Action_Helper'); // Routes if (isset($config->routes)) { $router = new My_Controller_Router_Rewrite(); $router->addConfig($config, 'routes'); $frontController->setRouter($router); } // Le modèle MVC devient affecté par Layout Zend_Layout::startMvc(array('layoutPath' => ROOT . '/application/modules/module1/layouts', 'layout' => 'main')); // Répertoire des controlleurs $frontController->addModuleDirectory(ROOT . '/application/modules'); // run ! $frontController->dispatch();
Bootstrap-v2.php
define('APP', realpath(dirname(__FILE__) . '/../')); set_include_path(APP . PATH_SEPARATOR . APP . '/library' . PATH_SEPARATOR . APP . '/application/modules/module1/models/' . PATH_SEPARATOR . get_include_path()); require_once 'Initializer.php'; require_once "My/Loader.php"; // Set up autoload. Zend_Loader::registerAutoload('My_Loader'); // Prepare the front controller. $frontController = Zend_Controller_Front::getInstance(); // Change to 'production' parameter under production environemtn $frontController->registerPlugin(new Initializer('test')); // Dispatch the request using the front controller. $frontController->dispatch();
Initializer.php
require_once 'Zend/Controller/Plugin/Abstract.php'; require_once 'Zend/Controller/Front.php'; require_once 'Zend/Controller/Request/Abstract.php'; require_once 'Zend/Controller/Action/HelperBroker.php'; /** * * Initializes configuration depndeing on the type of environment * (test, development, production, etc.) * * This can be used to configure environment variables, databases, * layouts, routers, helpers and more * */ class Initializer extends Zend_Controller_Plugin_Abstract { /** * @var Zend_Config */ protected static $_config; /** * @var string Current environment */ protected $_env; /** * @var Zend_Controller_Front */ protected $_front; /** * @var string Path to application root */ protected $_root; /** * Constructor * * Initialize environment, root path, and configuration. * * @param string $env * @param string|null $root * @return void */ public function __construct($env, $root = null) { $this->_setEnv($env); if (null === $root) { $root = realpath(dirname(__FILE__) . '/../'); } $this->_root = $root; $this->initPhpConfig(); $this->_front = Zend_Controller_Front::getInstance(); // set the test environment parameters if ($env == 'test') { // Enable all errors so we'll know when something goes wrong. error_reporting(E_ALL | E_STRICT); ini_set('display_startup_errors', 1); ini_set('display_errors', 1); $this->_front->throwExceptions(true); } } /** * Initialize environment * * @param string $env * @return void */ protected function _setEnv($env) { $this->_env = $env; } /** * Initialize Data bases * * @return void */ public function initPhpConfig() { date_default_timezone_set('Europe/Paris'); } /** * Route startup * * @return void */ public function routeStartup(Zend_Controller_Request_Abstract $request) { $this->loadConfig(); $this->initDb(); $this->initHelpers(); $this->initView(); $this->initPlugins(); $this->initRoutes(); $this->initControllers(); } /** * Load configuration * * @return void */ public function loadConfig() { self::$_config = new Zend_Config_Ini($this->_root . '/application/config.ini', $this->_env); $registry = Zend_Registry::getInstance(); $registry->set('config', self::$_config); } /** * Initialize data bases * * @return void */ public function initDb() { if (isset(self::$_config->db)) { $db = Zend_Db::factory(self::$_config->db); Zend_Db_Table::setDefaultAdapter($db); try { $db->getConnection(); } catch(Zend_Db_Adapter_Exception $e) { echo $e->getMessage(); } } } /** * Initialize action helpers * * @return void */ public function initHelpers() { // register the default action helpers Zend_Controller_Action_HelperBroker::addPath('../application/modules/module1/helpers', 'Zend_Controller_Action_Helper'); } /** * Initialize view * * @return void */ public function initView() { // Bootstrap layouts Zend_Layout::startMvc(array( 'layoutPath' => $this->_root . '/application/modules/module1/layouts', 'layout' => 'main' )); } /** * Initialize plugins * * @return void */ public function initPlugins() { } /** * Initialize routes * * @return void */ public function initRoutes() { if (isset(self::$_config->routes)) { $router = new My_Controller_Router_Rewrite(); $router->addConfig(self::$_config, 'routes'); $this->_front->setRouter($router); } } /** * Initialize Controller paths * * @return void */ public function initControllers() { $this->_front->addModuleDirectory($this->_root . '/application/modules'); } }
Merci
Hors ligne
J'ai fini par comprendre. J'ai le même soucis que sur le post d'ondex. Un initializer ne peut donc pas modifier le router. Or, c'est la préconisation de créer un bootstrap.
Hors ligne
Pages: 1