Bootstrapping modules

I’ve started to play with modules in a Zend Framework 1.8 application as the new autoloader means that all your model directories no long have to be on the include_path for autoloading to work. What I’m specifically interested in is being able to instantiate a model that is within a module from within another module.

Setting it all up isn’t that hard, but I couldn’t find a concise description, so these are my notes on it.

Start by creating a ZF application using the zf command line tool:

$ zf create project myproject

Don’t forget to put a copy of ZF 1.8 into the library directory or ensure that it is on the include_path.

We now need a module:

$ cd myproject
$ zf create module blog

This will create all the relevant directories in myproject/application/modules/blog. We create a simple model within the blog module:

File: myproject/application/modules/blog/models/Info.php


class Blog_Model_Info
    public function getInfo()
        return "This is the info about the Blog module";

The naming is important. First we have the module name, then we have the word “Model” then we have the name of the model itself. It is important that this model’s name matches the filename too.

We want to use this model within the index action of the Index controller like this:

File: myproject/application/controller/IndexController.php


class IndexController extends Zend_Controller_Action

    public function init()
        /* Initialize action controller here */

    public function indexAction()
        // action body
        $info = new Blog_Model_Info();
        $this->view->blogInfo = $info->getInfo();

I’ve included the entire class here; most of it is auto-generated, you just need to add the two new lines under the // action body comment. Having assigned something to the view, we should display it so we can prove it worked:

File: myproject/application/views/scripts/index/index.phtml

<?php echo $this->blogInfo; ?>

(Note that we replace the pretty ZF welcome page.)

At this point we get an error:
Fatal error: Class 'Blog_Model_Info' not found in
/usr/local/zend/apache2/htdocs/myproject/application/controllers/IndexController.php on line 14

This is because we haven't told the autoloader about our module's model's directory. This is done using Zend_Application's bootstrapping. There are two parts:

Firstly we have to add a line to application.ini enable modules at the end of the [production] section:

File: myproject/application/configs/application.ini

resources.frontController.moduleDirectory APPLICATION_PATH "/modules"
resources.modules[] = ""

Secondly, we need to add a Bootstrap class to our module:

File: myproject/application/modules/blog/Bootstrap.php


class Blog_Bootstrap extends Zend_Application_Module_Bootstrap


Again, the naming is important; the class name must be {module name}_Bootstrap and it must extend Zend_Application_Module_Bootstrap. It must be stored in a file called Bootstrap.php within the root of the module.

That's it. If you refresh the page, you'll get the data from the Blog module's Info model within the default module:


All in all, it's not difficult at all, but if you don't have those two lines in application.ini and define a module bootstrap class, then it doesn't work.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s