webdev - Zend Framework Tutorials
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)
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

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.
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. | < Prev |
|---|
My Twitter
- feed:
- follow:
- 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 0 tweets in past 30 days from atirjavid:
People talking about '@Linux':




















