Currently Online

We have 22 guests online
None

Login

             | 

Search

Like it? Share it!

A Modular Directory Structure Quickstart: Create Directory Structure

webdev - Zend Framework Tutorials

User Rating: / 22
PoorBest 

The following tutorial will consist of creating the following conventional modular directory sturcture and bootstrapping it via Zend_Application 1.8.x.It is broken up over four chapters.

This tutorial assumes one or more of the following:
  • You are familiar with PHP
  • You know Object Oriented Programming.
  • You can setup Apache (mod_rewrite), Virtual Hosts
  • You are familiar with a previous version of Zend Framework
  • You understand how the typical non-Zend_Application bootstrapping works
  • You are familiar with the conventional modular directory structure as per the ZF Documentation
  • You are having trouble bootstrapping your modular directory structure with Zend_Application
  • You are trying to start a new application using Zend Framework's Zend_Application component with a modular directory structure
  • You are having trouble setting up Zend_Application's out-of-box Resource Plugins
  • You are not using Zend_Tool (bin/zf.sh, bin/zf.bat, bin/zf.php)
(Zend_Tool currently does not fully support modules or modular directory structure, even though it is capable of creating modules, any views, actions or controllers created via Zend_Tool will not be created in the modules, but in application/controllers, views, etc. I would not use Zend_Tool anytime soon.)

If some of the above are true, the please keep reading.

I like to have a certain order of operation.  You can incorporate this into your existing application as well, that is when we discuss the .ini configuration directives.

  • Create application Bootstrap
  • Create module Bootstraps
  • Create controllers, actions, layouts, and view scripts.
  • Create config.ini file to setup the following resources:
    • Bootstrap Resource
    • Database Resource
    • Frontcontroller Resouce
    • Layout Resource
    • View Resource
    • Session Resource
    • Autoloader Resource

zf

As an example we are going to create the following directory structure and files. Keep in mind that this is 100% conventional modular directory structure, so we expect each module to be its own application with its own Bootstrap file.  However, the advisable method is to have a global data model directory in your application that all modules can share, as well as module specific data models, for when you need those.  I will discuss creating those after we setup our initial application and get it up and running.  Lets start...

First I am going to create public/index.php

<?php

// define these global path constants here
define( 'ROOT_PATH' , dirname( dirname( __FILE__ ) ) ) ;
define( 'LIB_PATH' , ROOT_PATH . '/library' ) ;
define( 'APPLICATION_PATH' , ROOT_PATH . '/application' ) ;
define( 'MODULE_PATH' , ROOT_PATH . '/application/modules' ) ;

// define the path for config.ini
define( 'CONFIG_PATH' , ROOT_PATH . '/application/config' ) ;

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

// set the php include path
set_include_path(
    LIB_PATH . PATH_SEPARATOR .
    get_include_path()
);

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    CONFIG_PATH . '/config.ini'
);
$application->bootstrap()
            ->run();


This was copied from the Zend Framework Reference Guide (or can be generated via Zend_Tool) for the most part, and I added a few constants to it.

The next thing we need to do is to actually create the Bootstrap in application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
   
}

 

The next task on our hands is to create individual Bootstraps for our modules so that module based resource plugins can be loaded.  This is important. Do not create a Bootstrap.php for the default module, as it is our default module already, it's bootstrap will be skipped and never executed.

create and/or edit application/modules/admin/Bootstrap.php

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap 
{
   
}

Now what we need to do is to create our Default Index Controller and our Admin Index Controller

create and/or edit application/modules/default/controllers/IndexController.php

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        echo "Index Action of Index Controller of Default Module";
    }
}

create and/or edit application/modules/admin/controllers/IndexController.php

class Admin_IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        echo "Index Action of Index Controller of Admin Module";
    }
}
You will notice that our admin's module's IndexController (and any other controllers in this module) is prepended with Admin_. The reason is that using the front contoller resource plug in for Zend_Application we are going to set the default module. The default controllers do not need to perpend the module name. Any other module that is not the default module will need its controllers to prepend the modulename before the controller name. For example, Admin_IndexController, Admin_LoginController, Users_SignupController, Help_SearchController, etc. Keep in mind that the module name must be perpended in controller classes.

Next we are going to create our default Error Controller.

create and/or edit application/modules/default/controllers/ErrorController.php

class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
        // Grab the error object from the request
        $errors = $this->_getParam('error_handler') ;

        // $errors will be an object set as a parameter of the request object, type is a property
        switch ($errors->type)
        {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

                // 404 error -- controller or action not found 
                $this->getResponse()->setHttpResponseCode(404) ;
                $this->view->message = 'Page not found' ;
                break ;

            default:

                // application error 
                $this->getResponse()->setHttpResponseCode(500) ;
                $this->view->message = 'Application error' ;
                break ;
        }

        // pass the actual exception object to the view
        $this->view->exception = $errors->exception ;

        // pass the request to the view
        $this->view->request = $errors->request ;
    }
}

Now we need to create our Layouts, and View Scripts:


create and/or edit application/modules/default/layouts/default.phtml

 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
	<head>
	    <?php $this->headTitle("Sample Modular Zend_Application Example"); ?>
	    <?php $this->headLink()->appendStylesheet('/styles/style_ie.css', 'screen, projection', 'IE'); ?>
	    <?php $this->headLink()->appendStylesheet('/styles/style.css', 'screen, projection'); ?>

	    <?= $this->headLink(); ?>
	    
	</head>
<body>
    <h1>Default layout</h1>
    <?= $this->layout()->content ?>

</body>
</html>

 

create and/or edit application/modules/admin/layouts/admin.phtml

  
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
	<head>
	    <?php $this->headTitle("ZF Sample Application Admin Backend"); ?>		
	    <?php $this->headLink()->appendStylesheet('/styles/style_ie.css', 'screen, projection', 'IE'); ?>
	    <?php $this->headLink()->appendStylesheet('/styles/style.css', 'screen, projection'); ?>
           
	    <?= $this->headLink(); ?>
	    
	</head>
<body>
    <h1>Admin layout</h1>
    <?= $this->layout()->content ?>

</body>
</html>

create and/or edit application/modules/default/views/scripts/index/index.phtml

<h3>Index View Script of Index Action of Index Controller of Default Module</h3>

 

create and/or edit application/modules/default/views/scripts/error/error.phtml

<h1>An error occurred</h1> 
<h2><?= $this->message ?></h2> 
<h3>Exception information:</h3> 
<p> 
    <b>Message:</b> <?= $this->exception->getMessage(); ?> 
</p> 
<h3>Stack trace:</h3> 
<pre><?= $this->exception->getTraceAsString(); ?></pre> 
<h3>Request Parameters:</h3> 
<pre><?php var_dump($this->request->getParams()); ?></pre>

 

create and/or edit application/modules/admin/views/scripts/index/index.phtml

<h3>Index View Script of Index Action of Index Controller of Admin Module</h3>
Alright guys, this is it for now, for tutorial # 1.  In the next tutorial I will talk about creating the front controller plugin that will change our modules.

My Twitter

atirjavid's avatar
Atir Javid atirjavid
  • bio:
    C.E.O Frosted Web, Social Media Marketing & Development | C.T.O. Sky Social Media Inc, former Debian contributor, maintainer and developer. Currently w/FreeBSD.
  • web:
  • location:
    Baton Rouge, LA. USA
  • updates:
    389
  • followers:
    136
  • following:
    15
Last 4 following:
runev's avatar emiltofan's avatar CalEvans's avatar fabpot's avatar
Loading...

Last 0 tweets in past 30 days from atirjavid:

People talking about '@Linux':

Quick Links

CopyLeft Notice

Distribution of the Original Works

You’re free to copy and redistribute the copylefted work — either by itself, or as part of a larger collection of works. All you have to do is (1) be honest - by clearly crediting me as the author, and only redistributing accurate copies of the original (But you don't REALLY have to :) hehe )- and (2) keep it free - by distributing the copy under the same copyleft license, and including either a copy of the license, or a reference to its URL (http://creativecommons.org/licenses/by-sa/1.0/legalcode).

You don’t have to copyleft your whole newsletter to reprint a single article of mine in it (although copylefting your whole newsletter is certainly worthwhile on its own merits). You only have to distribute the copy of my work under the copyleft terms. You’re also free to distribute abridged, translated, or otherwise edited copies of my works — but these count as derivative works, not copies of the original.

Derivative Works

You’re also free to make and distribute derivative works from the copylefted work (such as abridged or edited copies of original works, stories about characters introduced in the original works, etc.). All you have to do is (1) be honest — by clearly identifying your work as a derivative work, and clearly crediting me as the author of the original work on which it was based (But you don't REALLY have to do that either :) Hehehe) — and (2) keep it free — by distributing your derivative work as free content under the terms of the same copyleft license under which the original work was distributed. Please read the (The GNU General Public License ). Thank you very much.

Credit me or don't. Just please keep the information free and flowing.