PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Adding and creating resources

If you followed the directions above, then your bootstrap class will be utilizing a front controller, and when it is run, it will dispatch the front controller. However, in all likelihood, you'll need a little more configuration than this.

In this section, we'll look at adding two resources to your application. First, we'll set up your layouts, and then we'll customize your view object.

One of the standard resources provided with Zend_Application is the "layout" resource. This resource expects you to define configuration values which it will then use to configure your Zend_Layout instance.

To use it, all we need to do is update the configuration file.

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
; ADD THE FOLLOWING LINES
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

If you haven't already, create the directory application/layouts/scripts/, and the file layout.phtml within that directory. A good starting layout is as follows (and ties in with the view resource covered next):


<?php echo $this->doctype() ?>
<html>
<head>
    <?php echo $this->headTitle() ?>
    <?php echo $this->headLink() ?>
    <?php echo $this->headStyle() ?>
    <?php echo $this->headScript() ?>
</head>
<body>
    <?php echo $this->layout()->content ?>
</body>
</html>

At this point, you will now have a working layout.

Now, we'll add a custom view resource. When initializing the view, we'll want to set the HTML DocType and a default value for the title to use in the HTML head. This can be accomplished by editing your Bootstrap class to add a method:

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function 
_initView()
    {
        
// Initialize view
        
$view = new Zend_View();
        
$view->doctype('XHTML1_STRICT');
        
$view->headTitle('My First Zend Framework Application');

        
// Add it to the ViewRenderer
        
$viewRenderer Zend_Controller_Action_HelperBroker::getStaticHelper(
            
'ViewRenderer'
        
);
        
$viewRenderer->setView($view);

        
// Return it, so that it can be stored by the bootstrap
        
return $view;
    }
}

This method will be automatically executed when you bootstrap the application, and will ensure your view is initialized according to your application needs.

Zend Framework