These days there are lot of buzz about using framework in the PHP developer community. Framework or the tool towards rapid web application development. Framework was not a new thing to Java world or even Ruby. But in PHP it is a vogue concept. This is primarily for the reason that in PHP a single task can be accomplished in multiple ways and developers were used to prefer their own way of doing the task. So lack of common consensus was leading to thousands and thousands way of achieving targets. There were class repositories, there were code libraries but not any framework. Zend started a pioneering work in this regard and then came cakePHP in the line of ruby on rail. Today there are plenty of frameworks available, but unfortunately so far none of them are de-facto standard.
What is a framework?
Say, you are going to build a web application. Regarding the web applications there are three aspects of development. One is the code which will interact with the database. That is will store and retrieve data. Then there can be code to send mail, to send or receive XML requests from other server. The next set of code build the business logic, that is builds the right query to get the data suitable for the context. The third set is the display part, that is the UI which is presented before the user. So typically a web application works in this manner.
User Requests Page —> The Page builds the logic —> interacts with the code base to get the data —> formats the data with color, border etc and displays.
Now, if you are building one web application it is ok for you to build it any way you like. But if building applications are your breads and butter then writing the same code base again and again for different applications is a los making proposition. What if, we can separate out various layers of the application and can reuse some of the layers. If we can structure the layers and can freeze it and over that structure we can build thousands of applications by altering the business logic and User Interface.
The MVC architecture:
The Model-View-Controller architecture uses the same three tier model. Here the business layer is the controller. The requests first comes to the controller and the controller interacts with model and view layer to prepare data.
Using MVC with zend framework:
To understand the usage we first need to understand the zend framework and MVC and then we need to know how we can use these two. Zend framework comes with a folder named Zend which you will get after unpacking the downloads. This folder has some built in controllers which we can extend to build our controller logic. A controller can have several components. Like a login controller can have three components which are nothing but the action point of controllers. These components are login page, login processing and logout action. So in zend controllers and componets are separated out using ‘/’ . That is if a page request is abc.com/aa/bb then aa is the controller and bb is the component of the controller.
Installation: The installation of zend framework is easy and the common practice is putting the zend folder under a folder library. In this article we will discuss how we can keep all codes into the web folder so that we can use it in shared hosting too. But the ideal installation is keeping the fraework codes at the top of web folder.After installation you folder structure can be like this
/webfolder (document root)
/webfolder/library/zend
/webfolder/controllers
/webfolder/models
/webfolder/views
/webfolder/views/helpers
Now views/helpers folder will have all your javascript and css file and controllers folder will have all your controller script.
Create an .htaccess file in the webfolder and its content will be.
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule .* index.php [L]
This will ensure that all requests to the website arrives at index.php and is channelized through it.
Now create a file index.php in the web folder and put these lines there.
//We will load zend loader require_once "Zend/Loader.php"; Zend_Loader::registerAutoload(); //Create the front controller $frontController = Zend_Controller_Front::getInstance(); //error handling = true $frontController->throwExceptions(true); // set controllers directory $frontController->setControllerDirectory('controllers'); //send request to controller $frontController->dispatch();
So when the page request is abc.com/xxx/yy the zend front controller will pas the request to a file xxxController.php in the controllers folder. This suggest you should create a file xxxController.php in the controllers folder.
In that page your code will be
class xxxController extends Zend_Controller_Action { public function indexAction() { //this method will be called when the request is abc.com/xxx or there are no components. in that case default index component is called. } public function yyAction() { //our case, that is abc.com/xxx/yy . Here the xxx controller is passing the request to yy componet } }
Your views folder: Now the tricky thing. How the views will be controlled. For this in the views folder create a folder scripts and then create folder as per the controller name. So your views folder will have a scripts folder under it and then a xxx folder in the scripts folder. Each controllers folder will have an index.phtml file there and in this file your html will stay.
Now when you call abc.com/xxx/yy the request will pass to xxxControllers.php and then it will render the index.phtml under views/scripts/xxx folder.
This is simply an overview and then hundreds of questions still remained. like how can I use javascript? How can I controll views? What is the role of models folder? etc.
The space is so little to discuss all. But I will continue it as a series as I myself learn the entire art. I will be learning the framework and so you, with me.








Very useful text to get started with Zend framework