How to Create a Zend Framework Project that Uses Modules CodeUnit 01 APR 2011

If your website can be logically split into a number of different functionalities, it makes sense to group those functionalities accordingly. In Zend terminology we are talking about modules, literally the folder splitting of logically grouped pieces of functionality. However, by default a generated Zend Framework project isn’t modular and to get the thing properly modularized, here’s what you have to do.

Using Zend Framework 1.11.4:

First, create your project like you normally would via the command line interface:

zf create project moduleproject
cd moduleproject

Next, create your default module. This will house module that will handle all requests to the root of your site, in other words http://moduleproject/helloworld or http://moduleproject/

zf create module default

Now add the all important index and error controllers:

zf create controller Index index-action-included[=1] default
zf create controller Error -m default

Replace the contents of the generated error controller with the following contents: (c:moduleprojectapplicationmodulesdefaultcontrollersErrorController.php)

class Default_ErrorController extends Zend_Controller_Action

    public function errorAction()
        $errors = $this->_getParam('error_handler');
        if (!$errors || !$errors instanceof ArrayObject) {
            $this->view->message = 'You have reached the error page';
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $priority = Zend_Log::NOTICE;
                $this->view->message = 'Page not found';
                // application error
                $priority = Zend_Log::CRIT;
                $this->view->message = 'Application error';
        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->log($this->view->message, $priority, $errors->exception);
            $log->log('Request Parameters', $priority, $errors->request->getParams());
        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        $this->view->request   = $errors->request;

    public function getLog()
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasResource('Log')) {
            return false;
        $log = $bootstrap->getResource('Log');
        return $log;


Next create an error view that will be used by Error controller: (c:moduleprojectapplicationmodulesdefaultviewsscriptserrorerror.phtml)

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “”>
<html xmlns=””>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Zend Framework Default Application</title>
<h1>An error occurred</h1>
<h2><?php echo $this->message ?></h2>

<?php if (isset($this->exception)): ?>

<h3>Exception information:</h3>
<b>Message:</b> <?php echo $this->exception->getMessage() ?>

<h3>Stack trace:</h3>
<pre><?php echo $this->exception->getTraceAsString() ?>

<h3>Request Parameters:</h3>
<pre><?php echo $this->escape(var_export($this->request->getParams(), true)) ?>

<?php endif ?>


Finally, add the following line in the production section of your application.ini file: (c:moduleprojectapplicationconfigsapplication.ini)

resources.modules[] =

Set your environment so that errors are display and then first hit your project URL to make sure it is showing up correctly, i.e. http://moduleproject/. Next, hit a non-existent URL like http://moduleproject/fail and look at the resulting error page. If the request parameters reads array( … ‘module’ => default’ … ) then you’re finished.

You now have a completely modularized project, which you can add to by running

zf create module module2
zf create controller index -m module2


Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.

  • matthewmatician

    I followed your instructions, and get a Fatal Error, “Invalid controller class (“Default_ErrorController”)”. Any thoughts on why this happens? I’ve rechecked the class file, and it looks totally valid to me.

    • strick

      You need to add the following to the .ini:

      resources.frontController.defaultModule = “default”
      resources.frontController.moduleDirectory = APPLICATION_PATH “/modules”

  • Es Dreamstarzz

     what should we put on application.ini for module ..thanks

  • Jojobonheur

    I followed your instructions, and get a Fatal Error, Fatal error: Class ‘Admin_Form_Login’ not found in C:wampwwwgetapplicationmodulesdefaultcontrollersIndexController.php on line 14. could you help me please?

  • Thanks. Really, It has been very helpful.