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:
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:
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:
<?php echo $this->blogInfo; ?>
(Note that we replace the pretty ZF welcome page.)
At this point we get an error: