Skip Navigation LinksHome > Categories > Code from a Category

Guestbook for Zend Framework

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: 25481
Add Date: 02/01/2012
Zend Framework - an object-oriented web framework with an open-source PHP 5. Zend Framework is also often referred to as 'component library', because he has a lot of loosely connected components, which can be used independently from each other. But the Zend Framework MVC also provides detailed implementation, with the help of which you can build a structured application .. This Quick Start introduces you to several commonly used components, including Zend_Controller, Zend_Layout, Zend_Config, Zend_Db, Zend_Db_Table, Zend_Registry and a few helpers (helpers). Using these components, we can create a simple Guestbook application that uses a database for a few minutes.

Model - View - Controller

So what people are saying, referring to the MVC and why we should follow this pattern? MVC - is more than a three-letter acronym, it has become standard in the design of modern web applications. Code of most Web applications is divided into three components: presentation, business logic and data access. MVC pattern does an excellent job with this division. The end result is that code submission will be in one part of your web application, as well as business logic and data access code. Many developers are finding that a clear separation of code necessary to maintain the original application, organized, especially when more than one person working on this project.

Let's come off from the pattern, look at its individual parts.

Model - this is part of the application, which defines the basic functionality using a variety of abstractions. It can be defined by some business logic and data access patterns.

Representation clearly defines what the user sees. Usually controllers pass data presentation to display them in a certain format. This is a good place to insert HTML markup into your application.

Controller - binds the whole structure together. Also manages the models, which solves the presentation used to display data based on user request or other factors which transmits the data presentation, those who need it. Experts in the field recommend using MVC controllers minimum of code as far as possible.

Of course here we are talking about strict separation of the pattern, but it will help us to understand the ins and outs of our guest book, which we have gathered to write.

Create a project.

First you need to download the latest version of Zend Framework and extract it.

Installing Zend Framework

If you prefer, you can add a path to the library / framework just unpacked in the include_path of your php.ini. In order not to copy in every project of this library. In the folder bin / Zend Framework are 2 scripts: and zf.bat for Unix-based systems and Windows, respectively, when referring to that using an absolute path. Open Terminal (in Windows, Start -> Run, cmd). Sign in folder where you want to position your project and run by appropriate script: create project quickstart #Unix

zf.bat create project quickstart #Windows

Executing these commands will produce the basic structure of the application, including initialization of controllers, and presentations. The directory tree looks like after:

Now is the time to copy a folder with the libraries of Zend Framework library / Zend folder in our project / library /

Now our project is created, and we are ready to boot (Bootstrap), configuration and action controllers (Action Controllers).


Bootstrap class defines the initialization of resources and components. By default, initializes Front Controller, using the application / controllers / folder as the main controller (more on that later).



Typically, the configuration of the project is stored in the application / configs / application.ini and stores nesolko basic guidelines for setting parameters of the environment for PHP (enable or disable the error output, etc.), points the way to the class of the initial load (and the class name) and the path to the controllers . ; application / configs / application.ini

On some things still need to stay in more detail. First, when using the configuration in INI-file, you can use constants; APPLICATION_PATH - constant. And in addition to the declared sections: [production], [staging], [testing] and [development]. Last inherit from section [production]. This is a very convenient way to organize a separate configuration for a single stage design and build the application.

Action Controllers

Controller distributes requests between appropriate models, and presentations.

The controller must contain one or more methods of ending in «Action»; these methods will then be called via the web. By default, the Zend Framework URLs adhere to the scheme / controller / action, where the «controller» is the name of the controller (minus the suffix «Controller») and «action» indicates the action (minus the suffix «Action»).

Usually, you have IndexController, which displays the home page and ErrorController, which is used to catch things like HTTP 404 (controller or action not found) and an HTTP error 500 (error in the application).



Presentation scripts are at application / views / scripts /, where they are further divided by using the names of the controllers. In our example, we have IndexController and ErrorController, which respectively refer to the index / and error / podderiktorii in our scripts.

In these podderiktoriyah also see a folder named action, default, index / index.phtml and error / error.phtml.

Scripts consist of presentations and HTML markup tags to insert PHP directives.

Creating a layer

As you have noticed the view scripts in the previous section contain only fragments of HTML - not the full page.

Now we turn generated content in full HTML page, using the global layer (layout).

In order to take advantage of Zend_Layout we need to tell the original boot loader to use Layout resources by adding the following lines to the application / configs / application.ini in section [production]:

resources.layout.layoutPath = APPLICATION_PATH «/layouts/scripts»

Create a folder application / layouts / scripts.

You can also insert headers DocType in your application, this must be added by appropriate resource in the boot.

An easy way to add life to boot - this is a protected method that starts with _init. Since the we want to initialize the doctype, thus creating _initDoctype () method.

Using this method, we report mean to use the specified doctype. To initialize the resource type to add the following to application / cofigs / application.ini in section [producation] resources.view [] =

Thus, we initialize the view with no options.

Now we initialize Zend_Layout and set Doctype, you only create a layer

We omit all the content in terms of the layout view helper and get access to the key «content». We also use headLink () for easy insertion of tags into your template.

Now you can go to the address http://localhost and look at the page source, there you should see the XHTML header section body, head and title.

Create a table in the database and create a model

Zend_Controller_Front is such a thing as modules, which are mini applications. They are located in application and are prefixed by the name of the module.

Zend_Application_Module_Autoloader provides functionality needed to access the various resources in appropriate directories and provides a standard mechanism for naming. An instance of the class is created by default during the initialization of the boot, booted by default uses the prefix «Application». In our example the BEM model and the form will also begin with this prefix.

Let's look at something from'm guestbook. Usually guestbook entries'm from the comment, the time of its creation, and often with an email address. Since the We will store all data in the database, we will also add a unique identifier. Our application will be able to display all entries, save and print the new one you want. Just below is a simple model of the database.

get () and __set () - provide a convenient mechanism to access the individual properties of records.

find () and fetchAll () - allow access to individual records or all together.

At this point we can start thinking about the configuration database. First you need to initialize the Db resource. We set the parameters in the configuration file for the Layout and View resources, do the same for Db.

* Insert here the parameters to connect to the database

At this stage we need to create in that database table, which will be kept and all records. To do this, execute the SQL query.

We now have a fully working database and table in it for our application. Follow our steps will be to create the application code, including customization of the data source (in this example, Zend_Db_Table) and data converter to connect to the source. In the end, we'll create a controller that will work with this model and for the withdrawal of existing records in the database, and to create new ones.

We use the Table Data Gateway to connect our data source; Zend_Db_Table provides this functionality. Start by creating a class based on Zend_Db_Table in your application / models / DbTable /, the file will be named Guestbook.php

It is important to note that our class Application_Model_DbTable prefix matches the path of its location.

Now let's create a Data Mapper.

Now is the time to write a class to interact with GuestbookMapper ..

Now, in order to tie all these elements together, let's write the controller.

And of course the script to display an

Turning now to the link you will see http://localhost/guestbook/ records from the database.

Create a form

For our guest book is a must form for adding new records.

First, create a form class

The form consists of five elements: a field for entering the e-mail text box comments, CAPTCHA spam protection, email button and sewn from cross-site request forgery.

Next we add to our controller method GuestbookController signAction () to display the form.

Also, we definitely need to create a script to display the form application / views / scripts / guestbook / sign.phtml

Now, by clicking the link http://localhost/guestbook/sign you should see the form in appendix.

Congratulations, we just create a simple application ispolzyayu some of the frequently used components of Zend Framework.

p/pbrush: js_id; } public function setMapper($mapper) { $this-script type= The form consists of five elements: a field for entering the e-mail text box comments, CAPTCHA spam protection, email button and sewn from cross-site request forgery. syntaxhighlighter$method($value); } } return $this; } public function setComment($text) { $this-!-- application/views/scripts/guestbook/sign.phtml --

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.