• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Zend bootstrap
 

Zend bootstrap

on

  • 19,863 views

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

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

Statistics

Views

Total Views
19,863
Views on SlideShare
3,659
Embed Views
16,204

Actions

Likes
1
Downloads
0
Comments
0

4 Embeds 16,204

http://intra.neuros.fr 16065
http://www.neuros.fr 98
http://neuros.fr 31
http://www.scoop.it 10

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Zend bootstrap Zend bootstrap Presentation Transcript

      • Zend Bootstrap Votre serviteur : Pierre PINON
    • Plan
        I Description II Pourquoi mettre en place un Bootstrap ? III Principes, Comment ça marche ? IV Exemples V Conclusion
    • 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, ...)
      • 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
      • III Principes, Comment ça marche ?
      index.php Bootstrap.php - Mise en place du projet application.ini MVC config.ini
      • 1/ index.php
        <?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();
      • 2/ Vhost Apache
        <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>
      • 3/ application.ini
        [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
      • 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() { } }
      • 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
      • 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();
      • 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 &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)); }
        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'); } }
      • 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...)
      • Des questions ?