Zend bootstrap

20,752 views

Published on

Présentation de l'utilité et du fonctionnement des bootstrap avec Zend Framework

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
20,752
On SlideShare
0
From Embeds
0
Number of Embeds
16,211
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Zend bootstrap

  1. 1. <ul>Zend Bootstrap Votre serviteur : Pierre PINON </ul>
  2. 2. Plan <ul>I Description II Pourquoi mettre en place un Bootstrap ? III Principes, Comment ça marche ? IV Exemples V Conclusion </ul>
  3. 3. I Description <ul>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, ...) </ul>
  4. 4. <ul>II Pourquoi mettre en place un Bootstrap ? </ul><ul>- 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 </ul>
  5. 5. <ul>III Principes, Comment ça marche ? </ul>index.php Bootstrap.php - Mise en place du projet application.ini MVC config.ini
  6. 6. <ul>1/ index.php </ul><ul><?php define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); set_include_path(implode(PATH_SEPARATOR, array( realpath(dirname(__FILE__)) . '/../library', get_include_path() ))); require_once 'Zend/Application.php'; $application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini'); $application->bootstrap()->run(); </ul>
  7. 7. <ul>2/ Vhost Apache </ul><ul><VirtualHost *:80> ServerName exemple.lan AddDefaultCharset UTF-8 DocumentRoot /var/www/exemple SetEnv APPLICATION_ENV development <Directory /var/www/exemple> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost> </ul>
  8. 8. <ul>3/ application.ini </ul><ul>[production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 bootstrap.path = APPLICATION_PATH &quot;/Bootstrap.php&quot; resources.frontController.moduleDirectory = APPLICATION_PATH &quot;/modules&quot; resources.frontController.plugins.error = &quot;Zend_Controller_Plugin_ErrorHandler&quot; resources.frontController.plugins.auth = &quot;Services_Plugin_Auth&quot; resources.frontController.plugins.menu = &quot;Services_Plugin_Menu&quot; resources.frontController.plugins.layout = &quot;Services_Plugin_Layout&quot; resources.frontController.plugins.jquery = &quot;Services_Plugin_JQuery&quot; resources.frontController.throwExceptions = false autoloadernamespaces[] = &quot;Services_&quot; autoloadernamespaces[] = &quot;Doctrine_&quot; autoloadernamespaces[] = &quot;Forms_&quot; filepaths.configs.config = APPLICATION_PATH &quot;/configs/config.ini&quot; view.smarty.caching = 0 view.smarty.debugging = 0 view.smarty.allow_php_tag = 0 url.pel = &quot;https://secure.ogone.com/ncol/prod/orderstandard.asp&quot; [preproduction : production] url.pel = &quot;https://secure.ogone.com/ncol/test/orderstandard.asp&quot; [development : preproduction] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 resources.frontController.throwExceptions = true view.smarty.debugging = 1 </ul>
  9. 9. <ul>4/ Bootstrap.php </ul><ul><?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() { } } </ul>
  10. 10. <ul>IV Exemples </ul><ul>1/ Sans Zend Bootstrap <ul>- Juste un fichier index.php </ul>2/ Avec Zend Bootstrap <ul>- Un fichier index.php - Un fichier Bootstrap.php - Un fichier application.ini </ul></ul>
  11. 11. <ul>IV Exemple : Sans Bootstrap : index.php </ul><ul><?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'); </ul><ul>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(); </ul>
  12. 12. <ul>IV Exemple : Avec Bootstrap : Bootstrap.php </ul><ul><?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 &quot;Smarty-3.0.6/libs/Smarty.class.php&quot;; $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)); } </ul><ul>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'); } } </ul>
  13. 13. <ul>V Conclusion </ul><ul>1/ Sans Zend Bootstrap <ul>- Plus simple à mettre en place - Tout centralisé dans un seul fichier index.php - Difficile à maintenir </ul>2/ Avec Zend Bootstrap <ul>- 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...) </ul></ul>
  14. 14. <ul>Des questions ? </ul>

×