2. Plan I Description II Pourquoi mettre en place un Bootstrap ? III Principes, Comment ça marche ? IV Exemples V Conclusion
3. I Description Le Bootstrap est un fichier lancé au début de l’application qui initialise le projet. De façon générale, il permet : - D'initialiser le projet (Point d'entrée) - Définir une configuration de contexte (Dev, Preprod, Prod) - Paramétrage de Zend (Plugins, Helpers, Autoloaders, ...) - Amorcer les différents éléments (View, Layout, Form, Email, Bdd, ...)
4. II Pourquoi mettre en place un Bootstrap ? - Pour remplacer l'ignoble fichier index.php qui contient tout est n'importe quoi - Pour centraliser les paramètres internes dans un fichier de configuration (ini, xml, …) - Pour créer différents paramétrages selon le contexte - Pour faciliter la configuration des éléments avant leur utilisation et ainsi éviter les paramétrages en dur dans les contrôleurs/actions - Afin d'avoir un endroit organisé où sont initialisés les différents éléments de l'application
5. III Principes, Comment ça marche ? index.php Bootstrap.php - Mise en place du projet application.ini MVC config.ini
9. 4/ Bootstrap.php <?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { public function __construct($application) { parent::__construct($application); $config = new Zend_Config_Ini($this->_options['filepaths']['configs']['config'], 'app'); $this->setOptions($this->getOptions() + array('app' => $config->toArray())); } protected function _initQuelqueChose() { } protected function _initQuelqueChoseDAutre() { } }
10. IV Exemples 1/ Sans Zend Bootstrap - Juste un fichier index.php 2/ Avec Zend Bootstrap - Un fichier index.php - Un fichier Bootstrap.php - Un fichier application.ini
11. IV Exemple : Sans Bootstrap : index.php <?php // Racine du projet define('ROOT_DIR', realpath('..')); // Répertoires include set_include_path( ROOT_DIR.PATH_SEPARATOR. ROOT_DIR.'/library'.PATH_SEPARATOR. ROOT_DIR.'/library/PEAR'.PATH_SEPARATOR. ROOT_DIR.'/project/Classes'.PATH_SEPARATOR. ROOT_DIR.'/project/Modules'.PATH_SEPARATOR. ROOT_DIR.'/project'.PATH_SEPARATOR. ROOT_DIR.'/project/Classes/Doctrine/models'.PATH_SEPARATOR. ROOT_DIR.'/project/Classes/Doctrine/models/generated'.PATH_SEPARATOR. get_include_path()); // Initialisation de la session $exception = null; try { require_once('Zend/Session.php'); Zend_Session::start(); } catch (Exception $exception) { session_start(); } // Initialisation de la gestion d'erreurs require_once('NCore/Exception.php'); NCore_Exception_Manager::setUncatchedExceptionCallback('displayUncatchedException'); if ($exception !== null) Ncore_Exception_Manager::process($exception); // Initialisation de l'autoloader require_once('NCore/Autoloader.php'); require_once('Loader.php'); $autoloader = NCore_Autoloader::getInstance(); $autoloader->addLoader(array('Loader', 'formLoader')); $autoloader->addLoader(array('Doctrine_Core', 'autoload')); // Initialisation de la configuration NCore_Config_Cache::setPath(ROOT_DIR . '/temp/cache/config/.'); NCore_Config_Cache::enable(); NCore_Config::setConfigDir(ROOT_DIR . '/config') ; // Initialisation de Smarty require_once('NCore/Smarty.php'); Ncore_Smarty::setDirectoryCompile(ROOT_DIR.'/temp/templates_c'); NCore_Smarty::addDirectoryPlugin('Smarty/plugins/'); NCore_Smarty::addDirectoryPlugin('NCore/Smarty/plugins/'); NCore_Smarty::addDirectoryPlugin('NCore/Filigrane/Smarty/plugins/'); NCore_Smarty::addDirectoryPlugin('NCore/Version/Smarty/plugins/'); NCore_Smarty::addDirectoryPlugin('NCore/Xiti/Smarty/plugins/'); NCore_Smarty::addDirectoryPlugin('SmartyPlugin/'); Ncore_Smarty::addDirectoryPlugin('NCore/Form/Smarty/plugins/'); // Initialisation de la vue Smarty, et du rendu final via Smarty require_once('NCore/Smarty/View.php'); require_once('NCore/Smarty/Decorator/Response.php'); NCore_Smarty_View::initViewRenderer(); NCore_Controller_Front::getInstance()->setResponse(new NCore_Smarty_Decorator_Response(ROOT_DIR . '/project/Modules/default/views/main.tpl')); // Parametrage du Front Controller require_once('NCore/Controller/Front.php'); require_once('ZendPlugin/Init.php'); require_once('ZendPlugin/Menu.php'); $manager = NCore_Doctrine::connect(); $manager->setCharset('utf8'); NCore_Form::enableJQuery(); NCore_Form::setDefaultDecorators( array( array('ViewScript', array('viewScript' => 'default|form.tpl')), array('AjaxJQValidator') )); NCore_Form::setDefaultElementDecorators( array( array('ViewScript', array('viewScript' => 'default|formElementVertical.tpl')), )); Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view->addHelperPath('ZendHelperView', 'ZendHelperView'); Zend_Controller_Action_HelperBroker::addPath('ZendHelperCtrl', 'ZendHelperCtrl'); Zend_Controller_Action_HelperBroker::getStaticHelper('Auth'); NCore_Controller_Front::getInstance() ->throwExceptions(false) ->setModuleControllerDirectoryName('ctrl') ->addModuleDirectory(ROOT_DIR . '/project/Modules/') ->registerPlugin(new ZendPlugin_Init()) ->registerPlugin(new ZendPlugin_Menu()) ->registerPlugin(new ZendPlugin_Auth()) ->setBaseUrl() ->dispatch();
12. IV Exemple : Avec Bootstrap : Bootstrap.php <?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { public function __construct($application) { parent::__construct($application); $config = new Zend_Config_Ini($this->_options['filepaths']['configs']['config'], 'app'); $this->setOptions($this->getOptions() + array('app' => $config->toArray())); Zend_Registry::set('configs', $this->getOptions()); } protected function _initView() { include_once "Smarty-3.0.6/libs/Smarty.class.php"; $view = new Services_View($this->_options['paths']['modules'], $this->_options['paths']['smarty']['temp'], $this->_options['view']['smarty']); $view->getEngine()->addPluginsDir($this->_options['paths']['smarty']['plugins']); $view->doctype('XHTML1_STRICT'); $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); $viewRenderer->setView($view) ->setViewSuffix('tpl') ->setViewBasePathSpec($this->_options['paths']['modules']) ->setViewScriptPathSpec('/:module/views/scripts/:controller/:action.:suffix') ->setViewScriptPathNoControllerSpec(':action.:suffix'); } protected function _initZendx() { $view = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer')->view; $view->addHelperPath('ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper'); $view->jQuery()->setRenderMode(ZendX_JQuery::RENDER_JAVASCRIPT | ZendX_JQuery::RENDER_JQUERY_ON_LOAD); //Tout sauf l'affichage des styles et scripts } protected function _initLayout() { $view = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer')->view; $inflector = new Zend_Filter_Inflector(':script.:suffix'); $inflector->addRules(array(':script' => array('Word_CamelCaseToDash', 'StringToLower'), 'suffix' => 'tpl')); Zend_Layout::startMvc(array('layout' => $this->_options['filepaths']['form']['layout'], 'view' => $view, 'contentKey' => 'content', 'inflector' => $inflector)); } protected function _initForm() { $view = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer')->view; $view->addHelperPath($this->_options['paths']['form']['helpers'], 'Services_Form_Helper'); Services_Form::addDefaultPrefixPath('Services_Form_Element', $this->_options['paths']['form']['elements'], 'element'); Services_Form::addDefaultPrefixPath('Services_Form_Decorator', $this->_options['paths']['form']['decorators'], 'decorator'); Services_Form::addDefaultElementPrefixPath('Services_Form_Validate', $this->_options['paths']['form']['validates'], 'validate'); Services_Form::addDefaultElementPrefixPath('Services_Form_Decorator', $this->_options['paths']['form']['decorators'], 'decorator'); Services_Form::addDefaultElementPrefixPath('Services_Form_Filter', $this->_options['paths']['form']['filters'], 'filter'); Services_Form::setTplForm($this->_options['filepaths']['form']['tplForm']); Services_Form::setTplElement($this->_options['filepaths']['form']['tplElement']); } protected function _initHelpers() { //Helper d'action foreach ($this->_options['paths']['helpers']['actions'] as $module => $path) Zend_Controller_Action_HelperBroker::addPath($path, 'Modules_' . ucfirst($module) . '_Controllers_Helpers'); Zend_Controller_Action_HelperBroker::getStaticHelper('Auth'); Zend_Controller_Action_HelperBroker::getStaticHelper('Configs'); //Helper de vue $view = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer')->view; $view->addHelperPath($this->_options['paths']['view']['helpers'], 'Services_View_Helper'); foreach ($this->_options['paths']['helpers']['views'] as $module => $path) $view->addHelperPath($path, 'Modules_' . ucfirst($module) . '_Views_Helpers'); } protected function _initDoctrine() { Zend_Loader_Autoloader::getInstance()->pushAutoloader(array('Doctrine_Core','modelsAutoload')); Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE); Doctrine_Core::loadModels($this->_options['paths']['doctrine']['models_path']); $connection = Doctrine_Manager::connection($this->_options['app']['doctrine']['dsn']); $connection->setCollate($this->_options['app']['doctrine']['collate']); $connection->setCharset($this->_options['app']['doctrine']['charset']); } protected function _initEmail() { Services_EmailExad::setOptions($this->_options['app']['email']); Services_EmailExad::setPathTpls('default/views/scripts/emails'); } protected function _initEnums() { Services_Enums::setEnumsFile($this->_options['filepaths']['configs']['enums'], 'enums'); } }
13. V Conclusion 1/ Sans Zend Bootstrap - Plus simple à mettre en place - Tout centralisé dans un seul fichier index.php - Difficile à maintenir 2/ Avec Zend Bootstrap - Plus d'organisation - Une initialisation/configuration flexible (contextes) et harmonieuse (Une place pour chaque chose et chaque chose à sa place [ Samuel Smiles] ) - Un peu plus de travail en début de projet (Si peu...)