Zend FR

Consultez la FAQ sur le ZF avant de poster une question

Vous n'êtes pas identifié.

#1 06-04-2009 14:00:08

sekaijin
Membre
Date d'inscription: 17-08-2007
Messages: 1137

[proposition]construire un bootstrap configurable et réutilisable

bonjours à tous

je bricole la 1.7 et je suis partit du bootstrap fourni pas Zend dans QuickStart
ce qui me gêne dans ce truc c'est qu'on refait d'appli en appli toujours la même chose à quelques variations près.

sauf que en agissant ainsi on n'est pas à l'abri d'erreurs. je vous propose donc de faire un classe bootstrap avec deux méthode (à la base ini et run) et d'utiliser un fichier ini pour activer ou non telle ou telle fonctionnalité
on aurait ainsi un index.php qui ne change pas un bootstrap que l'on peut enrichir et réutiliser tout en gardant la possibilité de paramétrer la chose.

voici donc ce que j'ai commencé à faire
index.php

Code:

<?php
try {
    require realpath(dirname(__FILE__) . '../Application/BootStrap.php');
} catch (Exception $exception) {
    echo '<html><body><center>'
       . 'An exception occured while bootstrapping the application.';
    if (defined('APPLICATION_ENVIRONMENT') && APPLICATION_ENVIRONMENT != 'production') {
        echo '<br /><br />' . $exception->getMessage() . '<br />'
           . '<div align="left">Stack Trace:' 
           . '<pre>' . $exception->getTraceAsString() . '</pre></div>';
    }
    echo '</center></body></html>';
    exit(1);
}

BootStrap::ini();
BootStrap::run();

à la rigueur cela necessite juste d'aller chercher le fichier bottstrap ailleur s'iil n'est pas dans le dossier ../Application

app.ini (dans Application/config

Code:

[production]
#utiliser les layouts
layout=/layouts/scripts
#doctype des pages
doctype=XHTML1_STRICT
#utiliser les exceptions
exceptions=true

database.adapter       = "PDO_SQLITE"
database.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"

[development : production]
database.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"

[testing : production]
database.params.dbname = APPLICATION_PATH "/../data/db/guestbook-testing.db"

on pourrait ajouter ici autant de paramètre que d'éléments optionnel dans les bootstraps
mettre une valeur dans le fichier ini étant moins risqué que d'écrire le code correspondant

bien sur il faut faire la classe BootStrap mais une foit qu'on a écrit quelques ligne pour ajouter une fonctionnalité on peut l'utiliser dans une autre appli

Code:

<?php

/**
 * boot for Zend_Framework applications.
 *
 * this boot loader use 1 ini files located into your Application/Config directory
 * app.ini file 
 *  
 */
class BootStrap
{

    public static function ini()
    {
        // APPLICATION CONSTANTS - Set the constants to use in this application.
        // These constants are accessible throughout the application, even in ini 
        // files. We optionally set APPLICATION_PATH here in case our entry point 
        // isn't index.php (e.g., if required from our test suite or a script).

        defined('BASE_PATH')
            or define('BASE_PATH', dirname(dirname(__FILE__)));
        defined('BASE_URL')
            or define('BASE_URL', '/');
        defined('APPLICATION_PATH')
            or define('APPLICATION_PATH', dirname(__FILE__));
        
        defined('APPLICATION_ENVIRONMENT')
            or define('APPLICATION_ENVIRONMENT', 'development');
        
        set_include_path(realpath(APPLICATION_PATH) . PATH_SEPARATOR . realpath(BASE_PATH) . PATH_SEPARATOR . realpath(BASE_PATH . '/library') . PATH_SEPARATOR . get_include_path());

        // AUTOLOADER - Set up autoloading.
        // This is a nifty trick that allows ZF to load classes automatically so
        // that you don't have to litter your code with 'include' or 'require'
        // statements.
        require_once "Zend/Loader.php";
        Zend_Loader::registerAutoload();

        // FRONT CONTROLLER - Get the front controller.
        // The Zend_Front_Controller class implements the Singleton pattern, which is a
        // design pattern used to ensure there is only one instance of
        // Zend_Front_Controller created on each request.
        $frontController = Zend_Controller_Front::getInstance();
        
        // CONTROLLER DIRECTORY SETUP - Point the front controller to your action
        // controller directory.
        $frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');
        
        // CONFIGURATION - Setup the configuration object
        // The Zend_Config_Ini component will parse the ini file, and resolve all of
        // the values for the given section.  Here we will be using the section name
        // that corresponds to the APP's Environment
        $configuration = new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini', APPLICATION_ENVIRONMENT);

        // APPLICATION ENVIRONMENT - Set the current environment
        // Set a variable in the front controller indicating the current environment --
        // commonly one of development, staging, testing, production, but wholly
        // dependent on your organization and site's needs.
        $frontController->setParam('env', APPLICATION_ENVIRONMENT);

        // BASE_URL - Set the current baseUrl
        $frontController->setBaseUrl(BASE_URL);
        
        // LAYOUT SETUP - Setup the layout component
        // The Zend_Layout component implements a composite (or two-step-view) pattern
        // In this call we are telling the component where to find the layouts scripts.
        if (isset($configuration->layout)) {
            Zend_Layout::startMvc(APPLICATION_PATH . $configuration->layout);
            // VIEW SETUP - Initialize properties of the view object
            // The Zend_View component is used for rendering views. Here, we grab a "global"
            // view instance from the layout object, and specify the doctype we wish to
            // use -- in this case, XHTML1 Strict.
            $view = Zend_Layout::getMvcInstance()->getView();
            if (isset($configuration->doctype)) {
                $view->doctype($configuration->doctype);
            } else {
                $view->doctype('XHTML1_STRICT');
            }
        }

        // DATABASE ADAPTER - Setup the database adapter
        // Zend_Db implements a factory interface that allows developers to pass in an
        // adapter name and some parameters that will create an appropriate database
        // adapter object.  In this instance, we will be using the values found in the
        // "database" section of the configuration obj.
        if (isset($configuration->database)) {
            $dbAdapter = Zend_Db::factory($configuration->database);
        
            // DATABASE TABLE SETUP - Setup the Database Table Adapter
            // Since our application will be utilizing the Zend_Db_Table component, we need 
            // to give it a default adapter that all table objects will be able to utilize 
            // when sending queries to the db.
            Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
        }
        
        // REGISTRY - setup the application registry
        // An application registry allows the application to store application 
        // necessary objects into a safe and consistent (non global) place for future 
        // retrieval.  This allows the application to ensure that regardless of what 
        // happends in the global scope, the registry will contain the objects it 
        // needs.
        $registry = Zend_Registry::getInstance();
        $registry->configuration = $configuration;
        $registry->dbAdapter     = $dbAdapter;

        if (isset($configuration->exceptions)) {
            $frontController->throwExceptions($configuration->exceptions);
        }
    }

    public static function run()
    {
        // DISPATCH:  Dispatch the request using the front controller.
        // The front controller is a singleton, and should be setup by now. We 
        // will grab an instance and dispatch it, which dispatches your 
        // application.
        Zend_Controller_Front::getInstance()->dispatch();
    }
}

notez les

Code:

if (isset($configuration->database)) {...

qui permettent d'activer une section ou pas

en cumulant nos expérience on peut avoir un bootstrap utilisable dans de très nombreux cas
pour ce premier jet j'ai fais deux simple fonction mais pourquoi pas des membres privés et des méthodes privées comme par exemple un membre pour configuration et de methodes commes

Code:

   protected static function setDatabase()
   {
        // DATABASE ADAPTER - Setup the database adapter
        // Zend_Db implements a factory interface that allows developers to pass in an
        // adapter name and some parameters that will create an appropriate database
        // adapter object.  In this instance, we will be using the values found in the
        // "database" section of the configuration obj.
        if (isset($configuration->database)) {
            $dbAdapter = Zend_Db::factory($configuration->database);
        
            // DATABASE TABLE SETUP - Setup the Database Table Adapter
            // Since our application will be utilizing the Zend_Db_Table component, we need 
            // to give it a default adapter that all table objects will be able to utilize 
            // when sending queries to the db.
            Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
        }
   }

ceci permet de d'alléger la fonction ini

je suis conscient que nous n'aurons jamais un boostrap universel mais si on couvre 80% on aura déjà un très bel outil
A+JYT

Dernière modification par sekaijin (06-04-2009 14:08:31)

Hors ligne

 

#2 06-04-2009 14:20:56

mikaelkael
Administrateur
Lieu: Donges
Date d'inscription: 18-06-2007
Messages: 1176
Site web

Re: [proposition]construire un bootstrap configurable et réutilisable

Hello,

C'est pour ça que Zend_Application arrive avec la 1.8. J'ai fait ma première appli ce WE avec et c'est plutôt pas mal. Regardes l'incubator (code + doc) si tu ne veux pas attendre la fin du mois. Tout y est et même plus que ce que tu proposes wink

Edit: http://akrabat.com/2009/03/25/initial-n … plication/ ou http://framework.zend.com/wiki/display/ … n+Scholzen pour des exemples

A+

Dernière modification par mikaelkael (06-04-2009 14:23:24)


Less code = less bugs
Contributeur ZF - ZCE - ZFCE - Doc ZF (CHM & PDF) - Vice-trésorier AFUP 2011
Ubuntu 11.04 - ZendServer

Hors ligne

 

#3 06-04-2009 14:33:45

sekaijin
Membre
Date d'inscription: 17-08-2007
Messages: 1137

Re: [proposition]construire un bootstrap configurable et réutilisable

c'est amusant de voir que ce qu'on a fait plus ou moins bien avec la 1.0 il y a quelque temps déjà arrive dans l'incubateur

j'avais fais ça en dérivant le front contrôleur avec la 1.0
je me demandais comment le reproduire dans la 1.7 et voilà que ça incube dans la 1.8

A+JYT

Hors ligne

 

#4 06-04-2009 14:53:10

mikaelkael
Administrateur
Lieu: Donges
Date d'inscription: 18-06-2007
Messages: 1176
Site web

Re: [proposition]construire un bootstrap configurable et réutilisable

Hello,

Zend_Application et Zend_Tool devraient être les nouveaux composants phares de 1.8. Il y actuellement beaucoup d'activité (en nombre de commit) autour de Zend_Queue, Zend_Navigation, Zend_Tag ou Zend_View_Helper_Cycle.

Toutes les informations concernant ces composants sont disponibles à l'adresse : http://framework.zend.com/wiki/display/ZFPROP/Home

A+


Less code = less bugs
Contributeur ZF - ZCE - ZFCE - Doc ZF (CHM & PDF) - Vice-trésorier AFUP 2011
Ubuntu 11.04 - ZendServer

Hors ligne

 

#5 07-04-2009 10:29:19

Mr.MoOx
Administrateur
Lieu: Toulouse
Date d'inscription: 27-03-2007
Messages: 1444
Site web

Re: [proposition]construire un bootstrap configurable et réutilisable

Plus ça va, plus ma surcouche au ZF s'amaigrie big_smile

Hors ligne

 

#6 07-04-2009 11:23:56

keilnoth
Membre
Date d'inscription: 30-08-2008
Messages: 128
Site web

Re: [proposition]construire un bootstrap configurable et réutilisable

C'est une bonne chose. Je vais aller grailler dans l'incubateur un de ces soirs pour voir ce qui se profile et éviter de faire le travaille 2 semaines avant que ça sorte en live. smile


Quelques tutoriaux Zend Framework !

Hors ligne

 

#7 07-04-2009 11:51:32

mikaelkael
Administrateur
Lieu: Donges
Date d'inscription: 18-06-2007
Messages: 1176
Site web

Re: [proposition]construire un bootstrap configurable et réutilisable

Hello,

C'est dans le trunk depuis ce matin (Zend_Application, Zend_Tool, Zend_Navigation)

A+


Less code = less bugs
Contributeur ZF - ZCE - ZFCE - Doc ZF (CHM & PDF) - Vice-trésorier AFUP 2011
Ubuntu 11.04 - ZendServer

Hors ligne

 

Pied de page des forums

Propulsé par PunBB
© Copyright 2002–2005 Rickard Andersson
Traduction par punbb.fr

Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages