Today we are going to setup a Zend framework from scratch, this tutorial already assumes you have a WAMP/XAMPP/Zend Server, if not I’ll advise you to get one but I use Zend Server as it comes with the necessary extension, and "come on" ,its Zend Framework, the more reason should use Zend Server, additional features and extra stuff, now before we fire on, we need to ensure that we are running PHP 5.3 and higher ,you can still use v 5.2 but as of now PHP 5.3 is installed on servers so it’s really up to you.
NOTE: This tutorial shows how to install Zend on Window Environment.
Downloading Zend Library
Zend library can be downloaded from the website, navigate over to framework and download the version you need. However it’s possible you might already have a ZF library already with your server, as with Xampp and Zend Server it comes shipped in already, though I usually download the latest version, so for Xampp users you might need to check your folders and open readme.txt to see the ZF version you are using.
Why Zend Framework?
unlike statically typed languages like C# and Java, PHP is a loosely typed language, where you simply declare a variable and PHP automatically determines the variable type, PHP is really easy when it comes to building dynamic websites, infact chances are a large percent of websites running are built with PHP ; Wordpress , Joomla and even Facebook. It's quite easy for newbies to grasp, and simple websites could be created within days, back then PHP Codes and HTML were usually mixed together in a single page and database connection in the same page and really the web pages still worked, but on the longer run it made it extremely difficult to maintain, as each changes were made to the application.
Zend Framework has a structure that makes it easier for web developer to maintain and also comes along with lots of packaged components ready to use, the best part of Zend Framework is that some component can be used without necessarily importing the whole framework.
What is Zend Framework?
The Zend Framework is a PHP library for building PHP web application. It provides a set of components to enable you to build PHP applications more easily which will be easier to maintain and extend over the lifetime of the application.
“Culled from Zend Frameworks in Action”.
Model View Controller (MVC) ?
The MVC is a design pattern that separates an application into 3 sections, Model, View and the Controller, because this tutorial is not an in-depth guide on Zend’s internal operation, you can click this link to Zend’s theory of operation to gain a better understanding however I will explain a bit on the components of MVC.
In quick summary the model is where all your heavy logic takes place that are specific to the web application all the list of item retrieved from a store, discount item with 5% higher, usually Model is also associated with Data source, the model determines source of data e.g. retrieving data in XML,CSV,JSON,HTML,YAML. Zend has class “Zend_Db” which helps with Database Connection and Database Abstraction, another class the “Zend_Db_Table” which creates an object oriented interface to your table; it’s a really powerful concept, especially the ORM(Object Relational Mapping) libraries,these days are quite popular, you can also look at Doctrine another ORM library,but for this tutorial we will stick to Zend for beginner’s sake.
The view is the presentation or the GUI aspect of the application, this handle the display logic, for instance I create an application that serves html, and need to render an RSS Feed, the view that can be interchanged with a RSS view script, Zend has a class “Zend_View” with helper methods, “helpers” are additional methods that give useful codes to help with complex logic for templating, they are created to avoid repetition, although it’s possible for Smarty to be used as your “templating Engine”,but the Zend View is equally powerful for basic use. Another nice feature is the “Zend_Layout”, Zend Layout is to Zend as What Master Pages is to ASP.NET, and if you are not familiar with that either, it’s a design pattern that allows you to separate content of your web page into separate parts.
The controller handles task in routing and rendering the web pages according to requests, it is the component that delegates what actions to perform. E.g. a URL http://yourpage.com/about/ gets forwarded to the Front Controller which handles all requests, it decomposes the URL and calls the appropriate Controller Action, the controller “AboutController” will be called, because no action was specified it calls the default action index. If the URL was supplied http://yourpage/about/company/ the companyAction method in the AboutController will be called, the controller also handles other operations such as URL Rewriting through the help of Zend_Front_Controller_Route. you should note that Zend uses camel casing so each method should be like this "storeAction"
Creating your First Zend Project
We are going to use the Zend CLI tool “zf.bat” to help create our project, this is a very useful tools that creates your projects and adds controllers and other components to your project, however to get the Zf.bat to run, you need to adjust your environment variable, but to be quite sure that it runs, start your command prompt. And type “zf” you should see the ZF Command Line version with options listed.
However if you get a message saying program is not recognized, you will need to located the path where zf.bat is located, open your Zend Server / Xampp , in Zend Server installation it is located in ZendServer/share/Zendframework/bin, you will need to copy that location and open your Control panel click on System and click on Advanced system settings,
click on Environment variables. In the System variables click on path and add the location each entry should be ended with “;”. Save and close the command prompt and retry again the zf command again.
Creating a Zend Project with Command Tool
First open your command prompt and change to the localhost www directory or the htdocs depending on your webserver using the cd command, now type “zf create project HelloZend”
You should get some information after Zend has created the project for you, here’s a snapshot of mine.
My project name is “ValSucks” basically because i hate vals even though i get gifts :D
Directory Structure of Zend Framework
The application folder holds the model, controller, scripts, layouts, modules it holds the whole application logic to your website.
Doc refers to documentation you might feel the need to document.
The library folder is usually where Zend Framework is copied to, however since your server comes with Zend Framework you can omit that, however when you upload to your live server you should copy the framework here, I prefer to rely on this than the Server’s ZF.
test folder involves unit testing which we will look at that later.
So let’s open our project in the browser.
Apparently when running on your local server it’s a bit different to live server because only the public_html is usually exposed to the public and the application is a folder level above the public folder we need to change our Apache Directive to serve our Zend project directly, the ZF tool created an .htaccess that will serve all request to index.php in the public folder which bootstraps the whole project and the front controller.
Open your XAMPP/Zend Server and locate the apache folder and look for the conf folder and open the httpd.conf, I cannot express how delicate this issue is, if you have ever installed PHP and Apache manually you will know how much you should not mess up with this conf file, I recommend making a copy before editing it.
Search for the DocumentRoot directive mine is
DocumentRoot "C:\Zend\Apache2/htdocs" change it to DocumentRoot "C:\Zend\Apache2\htdocs\<your project folder>\public/" mine will be DocumentRoot "C:\Zend\Apache2\htdocs\ValSucks\public/" , you will need to restart apache to launch with this new settings, in XAMPP you can open the XAMPP Controller Panel to restart the server, Zend comes with a tool Zend Server locate this in control panel. You will get a Zend Community Edition page supply your password and simply click restart PHP at the bottom of the page or you can simply use the task manager in windows and under services stop and start the Apache 2.2 service.
Retype the url again : http://localhost/
Note: if you don't see any image don't panic, it retrieves the image from the net.
And you get a running Zend Webpage, however at this point you should note that Apache serves this page directly so you might not be able to work with other projects, so you can always revert with DocumentRoot Directive, in the next part we will look at the structure and quickly have some simple interface and usability of the page, till then take care and explorer the contents.