While working on a new project where my base was Zend Framework I desided to give the default layout for new ZF projects in Zend Studio for Eclipse a chance. While it does start a good layout for a project by generating the default code which is a real time saver I did notice some problems.
- All of the class files have the closing php tag (?>) on them followed by white space.
- Lack of a modules directory in the default project
- The Initializer Plug-in does not do some very basic things that should be done.
1. Closing php Tag
According to the PHP Coding Standard that is on the Zend Framework Wiki:
For files that contain only PHP code, the closing tag (“?>”) is to be omitted. It is not required by PHP, and omitting it prevents trailing whitespace from being accidentally injected into the output.
2. Lack of a modules direcory
I know this is easy to add if you know what you are doing with ZF but someone who is new to the framework they might find it hard to understand how to add support for the modules directory. It’s part of the proposed project structure in the ZF wiki.
3. Initializer Plug-in
This is where I have the biggest problem with the default project that is generated.
- The basic layout of the file is fine except for all the init*() methods return void where they should return the class object so that you can chain requests together in the routeStartup() method.
- It sets the controller directory last so if something throws an exception before before the controller path is set you end up seeing the exception about how ZF can not find the error controller instead of the actually exception thrown.
Let me know in the comments if you have any suggestions or additions to this.
Here is my updated Initializer.php file
< ?php
<?php
/**
* My new Zend Framework project
*
* $LastChangedDate$
* $LastChangedRevision$
*/
require_once 'Zend/Controller/Plugin/Abstract.php';
require_once 'Zend/Controller/Front.php';
require_once 'Zend/Controller/Request/Abstract.php';
require_once 'Zend/Controller/Action/HelperBroker.php';
/**
*
* Initializes configuration depndeing on the type of environment
* (test, development, production, etc.)
*
* This can be used to configure environment variables, databases,
* layouts, routers, helpers and more
*
*/
class Initializer extends Zend_Controller_Plugin_Abstract
{
/**
* @var Zend_Config
*/
protected static $_config;
/**
* @var string Current environment
*/
protected $_env;
/**
* @var Zend_Controller_Front
*/
protected $_front;
/**
* @var string Path to application root
*/
protected $_root;
/**
* Constructor
*
* Initialize environment, root path, and configuration.
*
* @param string $env
* @param string|null $root
* @return void
*/
public function __construct($env, $root = null)
{
$this->_setEnv($env);
if (null === $root) {
$root = realpath(dirname(__FILE__) . '/../');
}
$this->_root = $root;
$this->initPhpConfig();
$this->_front = Zend_Controller_Front::getInstance();
// set the test environment parameters
if ($env == 'test') {
// Enable all errors so we'll know when something goes wrong.
error_reporting(E_ALL | E_STRICT);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$this->_front->throwExceptions(true);
}
}
/**
* Initialize environment
*
* @param string $env
* @return void
*/
protected function _setEnv($env)
{
$this->_env = $env;
}
/**
* Initialize Data bases
*
* @return Initializer
*/
public function initPhpConfig()
{
}
/**
* Route startup
*
* @return Initializer
*/
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$this->initControllers()
->initHelpers()
->initView()
->initDb()
->initPlugins()
->initRoutes();
return $this;
}
/**
* Initialize data bases
*
* @return Initializer
*/
public function initDb()
{
return $this;
}
/**
* Initialize action helpers
*
* @return Initializer
*/
public function initHelpers()
{
// register the default action helpers
Zend_Controller_Action_HelperBroker::addPath( $this->_root . '/application/default/helpers', 'Zend_Controller_Action_Helper');
return $this;
}
/**
* Initialize view
*
* @return Initializer
*/
public function initView()
{
// Bootstrap layouts
Zend_Layout::startMvc(array(
'layoutPath' => $this->_root . '/application/default/layouts',
'layout' => 'main'
));
return $this;
}
/**
* Initialize plugins
*
* @return Initializer
*/
public function initPlugins()
{
return $this;
}
/**
* Initialize routes
*
* @return Initializer
*/
public function initRoutes()
{
return $this;
}
/**
* Initialize Controller and Modules paths
*
* @return Initializer
*/
public function initControllers()
{
$this->_front->addControllerDirectory($this->_root . '/application/default/controllers', 'default');
$this->_front->addModuleDirectory($this->_root . '/application/modules');
return $this;
}
}



Hi Jon, thanks for the posting these issues!
Regarding the “Lack of a modules directory”, one can use the Module Generation feature, this will do all the job for you! right click on the zf project, select “New” -> Zend Framework Item -> Choose to add module and follow the instructions.
an issue was submitted on the other two…
Roy,
Thanks for bringing that up as i knew that was there and use it all the time. What i was talking about was the base modules directory and then also telling the frontController where the modules directory in the initControllers() method.
public function initControllers() { $this->_front->addControllerDirecory($this->_root . '/application/default/controllers', 'default'); $this->_front->addModuleDirectory($this->root . '/application/modules'); }Jon