The application bootsrap file (application / bootstrap.php)

For a basic bootstrap there is already a lot of code inside. One thing i haven't included yet is the database initialization as well as the database profiler initialization, i will add that part in as soon as we need it...


To make our code more reusable we could write several application resources (that where introduced with zf 1.8) but i won't use them here because i want to keep my tutorial simple. I will perhaps write an article about how to create custom resouces later. There are already lots of official reources in the zend framework library /library/zend/application/resources/.

Ok lets start with our bootstrap, first we set the useDefaultControllerAlways parameter to false, because we don't want the dispatcher to redirect our visitors to a default module, everytime a controller can't be found. I dont think that of an SEO point of view its a good idea to enable this, because every invalid url pointing to a non existing controller would redirect the user to an existing page, search engines could think that you try to spam them with lots of duplicate content.

Finally we return the data, because perhaps we want to use the autoloader again somewhere in our app.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
  
/**
 * MAIN APPLICATION BOOTSTRAP
 *
 * @author weber chris
 * @version 1.0
 *
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
  
    /**
     * configuration variable
     *
     * @var Zend_Config_Ini
     */
    protected $_configuration;
  
    /**
     * AUTOLOADING
     *
     * @return Zend_Loader_Autoloader
     */
    protected function _initCore() {
  
        $autoloader = Zend_Registry::get('Autoloader');
        $autoloader->registerNamespace('My_');
  
        if (APPLICATION_ENVIRONMENT != 'production') {
            $autoloader->suppressNotFoundWarnings(false);
        } else {
            $autoloader->suppressNotFoundWarnings(true);
        }
  
        return $autoloader;
  
    }



As we need the configuration variables in some bootstrap methods, we take the configuration variables from the registry (we have loaded those in the public/index.php previously).

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * CONFIGURATION
 *
 * @return Zend_Config_Ini
 */
protected function _initConfig() {
 
    $this->_configuration = Zend_Registry::get('Configuration');
 
    return $this->_configuration;
 
}



Next step, now we force the frontcontroller being initialized by the zend framework frontcontroller resource. We set an header that will set the encoding of our pages to utf-8.

I choosed FrontControllerOutput as name to avoid conflicts with the FrontController method. In the second part of the FrontControllerOutput mthod we set the useDefaultControllerAlways parameter to false, because we don't want the dispatcher to redirect our visitors to a default module, everytime a controller can't be found. I dont think that of an SEO point of view its a good idea to enable this, because every invalid url pointing to a non existing controller would redirect the user to an existing page, search engines could think that you try to spam them with lots of duplicate content.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * FRONT CONTROLLER
 *
 * @return Zend_Controller_Front
 */
protected function _initFrontControllerOutput() {
 
    $this->bootstrap('FrontController');
    $frontController = $this->getResource('FrontController');
 
    $response = new Zend_Controller_Response_Http;
    $response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);
    $frontController->setResponse($response);
 
    $frontController->setParam('useDefaultControllerAlways', false);
 
    return $frontController;
 
}



This part adds the prefix of our action helpers to the helpers broker so that zend framework knows prefix of those classes and therefore where to find them.

1
2
3
4
5
6
7
8
/**
 * ACTION HELPERS
 */
protected function _initActionHelpers() {
 
    Zend_Controller_Action_HelperBroker::addPrefix('My_Controller_Action_Helper');
 
}



We now setup our logger, to log errors and other informations. The messages will get logged in a file called application.log and we will put it in our logs folder application/logs/.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * ERROR LOG
 *
 * to log errors in controllers use the logging action helper
 *
 * @return Zend_Log
 */
protected function _initLogging() {
 
    $logsDir = APPLICATION_PATH.'/logs/';
 
    if (!is_dir($logsDir)) @mkdir($logsDir, 0755);
 
    // init error logger
    $logger = new Zend_Log();
 
    $writer = new Zend_Log_Writer_Stream($logsDir.'application.log');
    $logger->addWriter($writer);
 
    return $logger;
 
}



In this part we set the encoding to utf-8 for the html code (or another encoding, but it should be the same you used in the header code you passed to the frontcontroller). As doctype we use html5 because for the layout we will use html5.

Next we add a default title (html title tag) for all our pages and we tell zend framework to use "-" as seperator. The title value we want to use is taken from the configuration file. Afterwards we will add a second part to the title which better describes the page the user is visiting, that part and the first part will be seperated by the seperator we have defined.

Finally we add a path and namespace for our global view helpers that we will put in the layout folder. Those helpers can be used by all the modules.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * VIEWS & LAYOUT
 *
 * @return Zend_View
 */
protected function _initViewHelpers() {
 
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $view->setEncoding('UTF-8');
    $view->doctype('HTML5');
 
    // enable dojo
    Zend_Dojo::enableView($view);
 
    $websiteTitle = $this->_configuration->website->title;
    $view   ->headTitle()->setSeparator(' - ')
            ->headTitle($websiteTitle);
 
    $view->addHelperPath(APPLICATION_PATH.'/layouts/helpers/', 'Application_Layouts_Helpers');
 
    return $view;
 
}



If you are in a controller action, a plugin or an action helper use the following code to retrieve data from one of our initialization methods, for example to retrieve the configuration object use this code:

1
2
3
4
5
<?php
  
$bootstrap = $this->getInvokeArg('bootstrap');
$configuration = $bootstrap->getResource('config');
$websiteTitle = $configuration->website->title;



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章