Skip Navigation LinksHome > Categories > Code from a Category

Introduction to the Zend Framework: Part Two.

User Name: serfcompany
Name: Serf
Contact Me:
Home Page:
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 1162
Add Date: 06/05/2012
In a previous article, I focused on creating boot file. Continue.

Let's start with a simple example: suppose we want to write the section "News" on our website. To do this we will need to create a database table in it, Controllers and view scripts (for Zend_View, but more on that later).

Create a database, and in order to avoid confusion in names, name it «zf_db» (note - all lowercase letters).
Create a database table called «news» with the following fields:

Title. Type. A value of zero? Notice.
id int (11) No Primary key, Autoincrement
title varchar (500) No
description text No
full_text text No

First you need to clearly define what functionality we put our application. On the home page we'll display a list of all the news site, with the ability to edit them.
There will also be key to create news and to remove it. As a result, our application should have a functional:

1.The conclusion of all the news site;

2.Add news;

3.Editing the news;

4. Removal of news;

Let's take a look at the basics of creating pages on the Zend Framework.

According to the accepted terminology, each page of a Web application is defined by the term "action".
Actions, in turn, are combined in a special group - "controllers."
The controllers are designed to bring together related activities.

In our case, the controller will be a «news», and four actions: add, edit, delete, index. The action index - an action which is performed by default (if the URL is not specified some other action).

In order to create the necessary controllers and actions, go to the folder «/ application / controllers /».
In Zend Framework controller is a class with a name like Imya_kontrollera {} Controller. The class must be described in the file named {Imya_kontrollera} Controller.php.
As I mentioned above, our controller will be called the «news», therefore, create a file «NewsController.php», and in it write the following code:

In this file, we declare a class NewsController, which is a subclass of Zend_Controller_Action library framework and specific methods of classes that end with the word «Action».
These methods and there are actions that I wrote above. That is, if you like, please contact http://site/news/add/ - «start» NewsController, action add (function addAction ()).

Everything. It remains only to fill the data of the code. What do we do now!

The database and configuration file

To store the settings of your site (setting for the database, passwords, directories) are very convenient to use the class Zend_Config and xml files. Zend_Config provides a convenient interface for reading configuration database and to use them.
There is also a class Zend_Registry, which will allow access to the variables from anywhere in our application.
To use all this beauty you want to append to the file index.php (prior to calling dispatch () Front Controller), the following lines:

In the first line, we load the configuration file.
The second - save . Zend_Config_Xml object to the registry (for details on these features, see the manual).
Next, we create a database adapter, passing to the constructor of the settings from the configuration file and also keep the adapter in the registry. Line «Zend_Db_Table :: setDefaultAdapter ($ dbAdapter);» establishes our database adapter, the adapter by default for all operations with the database.

Create a file «config.xml» (what to write - read the manual) in the folder «application».
All we have configured the database. We proceed to fill the code of our application.

We fill the application functionality.

Write this code in NewsController.php:

The code is very simple to explain why many will not (especially since it is well documented), if that - read the manual used in class - it's all very well written. Init () function is called when a class, so for convenience I have retained in the database adapter class variable.

I also used a very convenience function - _redirect (), which redirects the browser to the address assignments. Zend general logical concepts, the most difficult part of it - to start. Of course, you can tell me: "And what's the point of this framework?".

Let me explain: what you saw - a very trivial things and just in time, when you get to use, for example, classes Zend_Feed, Zend_Db_Table, Zend_Db_Table_Rowset or Zend_Db_Table_Row, then you will appreciate the power and convenience of Zend Framework.
Also, I advise you to draw your attention to the classes and Zend_Controller_Router_Route Zend_Cache.


In these examples, we have learned to work with you to controllers, actions, and perform primitive operations on the database. In the future, look at the basics of using classes Zend_Db_Table, Zend_Db_Table_Rowset and Zend_Db_Table_Row.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.