Zend Framework & MVC Introduction

June 9th, 2012 by Laeeq | 1 comment

Zend Framework is a popular php framework developed by supporters of PHP and Zend. it is an open source object oriented web application framework. it has flexible architecture with advanced Model-View-Controller (MVC) implementation that’s used to develop a basic structure for your Zend Framework applications. Zend Framework also supports web 2.0 and cloud computing technologies. The Zend Framework is supported many contributor like IBM and other major companies.

Model-View-Controller(MVC)

So first we will know about Model-View-Controller(MVC),  Now MVC pattern become a standard in the design of modern web applications. Most web application code falls under the following three categories: presentation, business logic, and data access.

Model – Data access routines and some business logic can be defined in the model.

View – Its a presentation layer. it define exactly what is presented to the user. actually controllers pass data to each view to render in some format.

Controller – it binds the whole pattern together. They manipulate models, decide which view to display based on the user’s request and other factors, pass along the data that each view will need, or hand off control to another controller entirely.

Design Structure of the Project

First you need to download latest version from below link.

ZIP File OR  TAR File

Now We will organize the file structure for our future project. Create two directories in the root of the application:

  • application – here all our software modules of the project will be stored
  • public- where will be available all share files.

 

Also we create index.php and .htaccess in the root, in which immediately add rules of redirects:

  1. RewriteEngine on
  2. RewriteRule .* index.php

It’s necessary to add similar .htaccess file, in the folder “public”.

  1. RewriteEngine off

Now We will create 3 folders in “application” directory:

  • configs — here is configuration files of the project.
  • library — for additional libraries.
  • modules — and it’s for modules of our application.

 

After all these simple manipulations I had such a structure:

The root/

  • application
  • configs
  • library
  • modules
  • public
  • .htaccess
  • .htaccess
  • index.php

 

Now our project structure is ready, so we can move on to the coding

We will update index file (index.php) with the 4 constants.

  1. define(‘PATH_TO_ZF’, ‘../../../ZF/1.11.11/’);
  2. define(‘PATH_TO_APPLICATION’, ‘./application/’);
  3. define(‘PATH_TO_LIBRARY’, PATH_TO_APPLICATION . ‘library/’);
  4. define(‘PATH_TO_MODULES’, PATH_TO_APPLICATION . ‘modules/’);

Now we will set path to load all our files:

  1. include_path(PATH_TO_ZF . PATH_SEPARATOR . PATH_TO_APPLICATION . PATH_SEPARATOR . PATH_TO_LIBRARY);

The next step is to download Zend_Loader

  1. require_once ‘Zend/Loader.php’;
  2. Zend_Loader::registerAutoload();

So, Zend_Loader is loaded, now let’s activate Zend_Controller_Front and point the location of our modules to the dispatcher.

  1. $controller = Zend_Controller_Front::getInstance();
  2. $controller->addModuleDirectory(PATH_TO_MODULES)->dispatch();

The result file should be something like this:

  1. define(‘PATH_TO_ZF’, ‘../../../ZF/1.11.11/’);
  2. define(‘PATH_TO_APPLICATION’, ‘./application/’);
  3. define(‘PATH_TO_LIBRARY’, PATH_TO_APPLICATION . ‘library/’);
  4. define(‘PATH_TO_MODULES’, PATH_TO_APPLICATION . ‘modules/’);
  5. set_include_path(PATH_TO_ZF . PATH_SEPARATOR . PATH_TO_APPLICATION . PATH_SEPARATOR . PATH_TO_LIBRARY);
  6. require_once ‘Zend/Loader.php’;
  7. Zend_Loader::registerAutoload();
  8. $controller = Zend_Controller_Front::getInstance();
  9. $controller->addModuleDirectory(PATH_TO_MODULES)->dispatch();

As you can notice Zend_Controller_Front never loaded because Zend_Loader loadsController automatically.

Developing our Module

Now we will create module folder in our modules folder named as “Index”.

Now we will create two more folders in our module’s folder:

  • controllers — there are module controllers
  • views — but here everything related to the view (presentation)

Create a new controller (IndexController.php), and put this code:

  1. class IndexController extends Zend_Controller_Action
  2. {
  3. public function indexAction()
  4. {
  5. return;
  6. }
  7. }

Now you need to create a script for our controller. For it, create folders “scripts/index” in a folder “views”. It should be something like this:

  • default
  • controllers
  • IndexController.php
  • views
  • scripts
  • index

Create a file index.phtml in the folder views/scripts/index/

  1. $this->view->var = ‘ZendFramework’;

General view of the controller looks like this:

  1. class IndexController extends Zend_Controller_Action
  2. {
  3. public function indexAction()
  4. {
  5. $this->view->var = ‘ZendFramework’;
  6. }
  7. }

Lets add into the view file with the following code:

  1. echo $this->var;

After that, you can update the page.

So, we have discussed a lot of interesting things about ZF. In this article I mentioned that ZF is based on the MVC architecture. If you have still queries, you can read basis of MVC architecture.

 

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Top 5 PHP Frameworks 2012
  •  

     

    1. September 27th, 2012 at 13:20 | #1

      I simply could not depart your website before suggesting that I really loved the usual information an individual supply to your visitors? Is going to be again steadily in order to check up on new posts.
      http://uggoutlet1.webstarts.com

    1. No trackbacks yet.