It’s a bit fidgety setting up Zend Framework for use in a modular application, but the Zend Tool takes away a lot of the pain and misery!

zf create project whatever
cd whatever
zf create module default
zf create module other
zf enable layout

now edit your index.php in the public folder and change from production to development mode:

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

The order of the application.ini settings appear to be of great importance. This worked for me:

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" 
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" 
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.modules[] =

It won’t find the error controller and we need our default index controller:

zf create controller index index-action-included[=1] default
zf create controller error index-action-included[=1] default

The default module doesn’t require any namespace, so class Default_IndexController should be changed to just Index_Controller,
and the same goes for the Default_ErrorController.

Copy and paste the error() method from the original error controller in application/controllers, and paste it in to /application/modules/default’s error controller. Do the same for the view file.

Make a slight edit to the new error controller’s view file.

<h1>An error occurred!</h1>

The exclamation will help us know if the modules error controller is being used or not.

Replace the content of the default modules index view with some text (Hello Modular World?) to distinguish it apart from the original index controller. Do the same (Hello different module)  for the index viewof the ‘other’ module.

Now browse to your site, and hopefully you won’t be seeing the Welcome to Zend Framework message of the original index controller’s view file, and instead see your hello world message. add /other on to the end of your url, and try that. Your other message should appear. Finally mess with your url and try to go to /this/will/never/work. You should see the error message with the exclamation.

Finally lets get the layout stuff happening. go in to /application/layouts/scripts/layout.phtml and add something:

<h1>Awesome!</h1>
<?php echo $this->layout()->content; ?>

And that’s us good to go! Using modules in Zend Framework is a great way of creating great reusable code, and by nature helps keep your code nice n loosely coupled.

Advertisements