A Complete Overview of Zend Technologies: A full stack PHP Framework
Created by Zend Technologies, Zend Framework is a full-stack PHP framework which is made up of many different libraries and classes m kinds of applications and services such as Zend Server, a web application server for running and managing PHP application it easier for developers to cherry-pick libraries if they need only few.
Although, Zend Technologies company offers various technologies like Zend Server, a web application server for running and managing PHP applications, and Zend Studio, a full-featured IDE for PHP and many others, but Zend Framework has its own significance and importance as it is a great tool which add bulk to any developer’s arsenal.
Basically, Zend Framework is an open source, object oriented web application framework which is often termed as a ‘component library’ just because it has many loosely coupled components which can be used more or less independently. Especially designed for PHP 5, Zend Framework provides an advanced Model-View-Controller (MVC) implementation which can be used to establish a basic structure for Zend Framework applications. Here, your curiosity can grow to know about Model-View-Controller (MVC) pattern which is quite obvious. MVC has actually become the standard in the design of all the modern application and you often find everyone talking about this crucial topic.
We are hereby in this post detailing the functionality of Zend Framework in detail, i.e how it works and how one can use its various components. We will show you how to create a project in Zend framework. Let us begin with the how to download Zend Framework. We will move ahead step by step.
Step 1: Download Zend Framework
The first and foremost thing which comes when you decide to create any project in Zend Framework is the download of this particular framework and then setting a working environment there. Let us download the current version of Zend Framework, i.e. 1.11.11, by visiting to its official site http://framework.zend.com and clicking on “Download Now” button as show below.
Once you click on this link, it will bring you to a screen with a list of different downloads for Zend Framework; here choose the minimal version, i.e. Zend Framework 1.11.11 Full. As soon as you click there, your downloading starts.
Step 2: Set up your Zend Framework Environment
Once the download gets completed, there comes need to set the Zend Framework environment and make it ready to work. For this you need to extract the files, basically the “bin” and “library” folder, to your desktop in order to get a clear view of the contents. Inside, there are a lot of folders, but for the purposes of this tutorial, we will only need two — the bin folder and the library folder.
For sake of organization let us put the “bin” folder inside the PHP folder and then renaming it to Zend-bin. Here, in this post we have shown the folder putting in D:\Development\PHP, hence ultimately our output location will be D:\Development\PHP\Zend-bin. This is shown clearly as below:
Now, go into the “library” folder, find the “Zend” folder and place it inside the PHP’s include_path. Thus in this case our required location will be D:\Development\PHP\includes\Zend.
Finally, it is required to configure the Zend CLI tool inside the Zend-bin folder so that it may work in any directory. To accomplish this, here, we have added the directory to the “Path” System variable, so that it can further be called via the command prompt. “Path” variable is set just by right clicking the “computer” link under “Start” button and then opening the “System Information Window”; after then clicking on “Advanced System Settings”.
After clicking on “Advanced System Settings” a new window appears; in that window click on the “Environment Variables” button, then look for the “Path” variable in the “System Variables” list. In order to edit that path click on “Edit” and add your “Zend-bin” directory location at the end. After getting done, just click on the “OK” button.
Now, it is required here that whatever changes you have made, or path you have configured is whether working or not. In order to ensure that the configuration is working, first of all open a command prompt and then run the ZF–HELP command. This will make you sure that the System variables are implemented.
Step 3: Create your First Zend Framework Project
Once download and setting of development environment has been completed, now, its time to begin actual development work on Zend Framework project. To get started, in the command prompt, go to the directory where you want to put your project files. It should be noted that ideally this should be the directory where your local web server points to; here in this case it is D:\Development\htdocs\nettuts.
Now, in command prompt, following command should be typed to create base Zend Framework project, i.e. ZF Create Project Thenextsocial where “Thenextsocial” is the name of the project.
Now when you will check the location mentioned as above, you will find a new Thenextsocial folder containing multiple project files. Also there will be a “Public” folder which will be the only publicly accessible folder via browser. To make this folder accessible, there is needed to set up a VirtualHost for the project.
To set up a VirtualHost, the following given code inside the httpd.conf file of Apache’s should be added.
It is also needed to add the domain to the local hosts file. In general, for Windows users, it should be in C:\Windows\System32\Drivers\etc. And for the Unix users, it should be in /etc/hosts. This folder should be opened and thenextsocial.social should be added to it and pointed it to 127.0.0.1.
Now the web server should be restarted; when http://thenextsocial.local is opened on the browser, it should definitely point to the Zend Framework project Restart your web server. If it is so, the first Zend Framework project has been successfully created.
Step 4: Learn about Zend Framework’s MVC Structure
Generally, we see that, most web application code falls under any one of the three categories including presentation, business logic, and data access; the MVC models does this separation well keeping the code in organized manner. It has been found as the well-defined separation indispensable for keeping the code organized, especially when more than one developer is working on the same application. Let us see it compartmenting into individual pieces, i.e. Model, View and Controller as clearly shown in the figure given below:
➡ Model – Model is that part of any application which defines the basic functionality behind a set of abstractions; it is here, where data access routines and some business logic can be defined.
➡ View – Views define exactly what is presented to the user; it is the place where user can find find HTML markup in his/her MVC applications.
➡ Controller – Controllers are the entities which bind the whole pattern together manipulating models and decide which view to be displayed depending upon the user’s request and other factors. It passes along the data needed by each view or hand off control to another controller entirely.
MVC in a Zend Framework-powered application
One of the most noticing thing about using the ZF CLI tool is that it automatically sets up your application to make use of Zend Framework’s MVC classes, which in turn allows to begin working on projects more quickly and efficiently. Let us explain Zend Framework’s default application routing.
By default, all URLs on Zend Framework project follow a certain format as shown below:
[url]/[controller]/[action]/[GET parameter 1 value]/…/[GET parameter x]/[GET parameter x value]
Another great thing is that is that if a controller and/or action is not provided, they both default to index. Thus calling http://thenextsocial.local will be the same as calling http://thenextsocial.local/index/index or even http://thenextsocial.local/index.
Let’s have a look over the pictorial representation of Zend Framework default routing cycle as shown below:
After then, Zend Framework takes the controller value and looks for it in the “Controllers” folder, namely, IndexController.php file inside which it takes the action value and executes the indexAction. Once the action will be executed in the controller, ZF automatically renders the view file inside the views folder in the following format:
This is the reason why we see the Welcome to Zend Framework page, since index.phtml contains the HTML code for this page.
Step 5: Pass Data From a Controller to its View
After being familiar with the Zend Framework default routing cycle and MVC structure, you must know how to pass data from a controller to its view. As soon a controller object is created, a view object is needed to instantiate it. This view object is accessed via the $this->view accessor.
Now, to send data to the view, we need simply to assign variables to the view object and give them values, like as presented below:
$this->view->variable_name = “variable value”;
Now let’s pass the current date and time to our view in the indexAction method in teh IndexController.php file of controller folder. It is shown as below:
public function indexAction()
$this->view->current_date_and_time = date(‘M d, Y – H:i:s’);
Now, it is needed to edit the view to output this variable for which, following given code is added to the open index.phtml file in the view folder:
<h1>Welcome to the <span id=”zf-name”>Zend Framework!</span></h1>
<hr /><h2>It is now <?php echo $this->current_date_and_time; ?></h2>
<h3>This is your project’s main page</h3>
As you can see, accessing the variable from inside the view file is as simple as echoing the variable via the echo PHP command,
Now, refresh your browser, and you should see the current date and time on the page.
Step 6: Create a Layout in Zend Framework
Generally, it is seen that the html scripts in any project are just fragments and not complete HTML pages, and thus they lack HTML, HEAD, and BODY tags, and in this way, all actions return content relative only to the action itself, not the whole application. Global Layout enables us to implement a consistent HTML container for all our HTML scripts; so let us create a layout in Zend Framework which will serve as the global layout.
zf enable layout
To begin, let us open our command prompt and open the thenextsocial folder and issue the following command. It will enable a global layout in Zend Framework project.
After running this command, whatever the output comes suggests that a layout file called layout.phtml has been created inside the application/layouts/scripts folder. Let’s open it up and take a look what’s is inside it:
<?php echo $this->layout()->content;?>
The command echo $this->layout()->content; is actually the layout file which echoes all of the content from the view. Now, in order to create the global layout it is needed to wrap this command with HTML. Just see the below given code snippets:
<title>The Next Social</title>
<!– Load Twitter Bootstrap and jQuery –>
<link rel=”stylesheet” href=”http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css”>
<p>This is the header!</p>
<!– Echo our content here! –>
<?php echo $this->layout()->content; ?>
<p>This is the footer!</p>
Now, refresh the browser once again and compare the changes with the earlier one and the present one. Here you can find index action accompanying well with layout.
Step 7: Create New Controllers and Actions
If you are learning about Zend Framework, it is important to learn creating new controllers and actions using the ZF CLI tool. Let us do it simply by running the following command:
zf create controller About
Here, “About” is the new controller’s name. If you want to ensure this, just check inside the controllers folder, you will find an AboutController.php file, indicating the controller generation successful. The contents of the views folder can also be checked and seen that a new about/index.phtml file has been created for the Aboutcontroller.
By default, the ZF CLI tool creates one action for controller, the indexAction. But if you are desirous to add more actions to the controller, you can do it easily; let us discuss this below:
Creating a new action
In fact, creating a new action is almost easy than creating a new controller; only there is a need to run the following command:
zf create action us About
This command effectively creates a new action called “us” inside the About controller which you can be ensured of by checking theAboutController.php file, where you will find new usAction() method, as well as a new us.phtml file inside the about view folder.
Now, it is proven that ZF CLI tool plays a major role and we heavily rely on this tool to generate actions and controllers. It can also be done manually but the presence of the ZF CLI tool just makes it simpler and easier.
Going through this blog post, you might have become aware of several facts relating to Zend Framework such as where to download the latest Zend Framework files, where and how to set it up locally, creating your first Zend Framework project and setting up a VirtualHost on your web server, how exactly Zend Framework implements the MVC pattern and its default application routine and so on. In this blog post we have made our best attempts to acknowledge you regarding passing data from a controller to its view, creating a site-wide layout for your Zend Framework application, creating new controllers and actions and many more. We hope these are the enough information which will enable you to create static web pages. But, it is not enough to build a dynamic web page; for this you need to go through further study.
Please do not forget to share your views with us; feel free to leave your comments in the comments section of this blog post.