Grow with Zend Framework (1)

Source: Internet
Author: User
Tags download zend zend framework

Model-View-Controller Architecture
The following is a traditional method for building PHP applications:

With the passage of time, when customers constantly have new requirements, such code-based applications distributed in different places become unmaintainable.
One way to improve the maintainability of an application is to separate the code into three completely different parts (and usually separated into different files ):

Model: A model is a program that processes the displayed data. In the above example, it is the concept of "news. In this way, the model is generally
Focus on the "Business" logic and read and store data from the database.
View: The view includes some programs that are displayed to the user, usually html
Controller: the controller works with the model and view to display the correct data on the appropriate page.

The Zend framework uses the Model-View-controller (MVC) architecture. This is used to separate out the different parts of your application to make development and maintenance easier.

Zend framework uses the Model-View-controller (MVC) architecture. This is used to separate your program into different parts, making development and maintenance easy.

Requirement
To run Zend framework, you must:

  • PHP 5.1.4 (or higher)
  • The Web server supports mod_rewrite. Apache is used in this tutorial.

Get Zend Framework
Download Zend frameworkfrom the http://framework.zend.com/download here, with two format .zip example .tar.gz. The current version is 0.9.1. For this tutorial, you must use version 0.9 or later.

Directory structure
Although Zend Framework does not require a standard directory structure, its manual recommends a general directory structure. This directory structure assumes that you have full control over Apache configuration, but we want to make it easier, so we made some modifications.

Create a root directory named zf-tutorial. This means that the URL pointing to the program will be http: // localhost/zf-tutorial. Create the following sub-directories to store the files required by the Program:

Zf-tutorial/
/Application
/Controllers
/Models
/Views
/Filters
/Helpers
/Scripts
/Library
/Public
/Images
/Scripts
/Styles

As you can see, we have separated the model, view, and controller files in our program into different subdirectories. Supported images, scripts, and CSS files are stored in different subdirectories under the public directory. The downloaded ZendFramework file is placed in the library directory. If you need other library files, you can put them here.

Decompress zendframwork-0.9.1-beta.zip to a temporary directory. All files are included in a directory called ZendFramework-0.9.1-Beta. Copy files from the ZendFramework-0.9.1-Beta/library to zf-tutorial/library. In this directory, there is a subdirectory named Zend.

Bootstrapping)
Zend Framework controller, Zend_Controller, is designed to support websites that use clean urls. For this purpose, all requests need to go through a single index. php file, which is known as the boot file (bootstrapper ). This provides the center of all pages in the program and ensures that the runtime environment is correctly configured. We use the. htaccess file to achieve this goal. htaccess is in the root directory of zf-tutorial. The content is as follows:

QUOTE:

Zf-tutorial/. htaccess
RewriteEngine on
RewriteRule. * index. php
Php_flag magic_quotes_gpc off
Php_flag register_globals off

RewriteRule is very simple and can be translated as "redirect to index. php for any url ".

For security and stability, we also made some settings in PHP ini. Although these settings are correct, we still need to confirm it! Note that the php_flag in. htaccess only works under mod_php. If you use CGI/FastCGI, make sure that the php. ini configuration is correct.
However, for images, JavaScript and CSS file requests should not be redirected to the Startup File. Put these files in the public directory, and we can easily configure Apache through another. htaccess file. The. htaccess file is in zf-tutorial/public:

QUOTE:

Zf-tutorial/public/. htaccess
RewriteEngine off

Although our current rewrite rules does not need to be too strict, we still add some. htaccess files in the application and library directories to protect our programs:

QUOTE:

Zf-tutorial/application/. htaccess
Deny from all

Zf-tutorial/library/. htaccess
Deny from all

Note: To set the. htaccess file in Apache, AllowOverride in httpd. conf must be set to All. The idea of setting multiple. htaccess files comes from Jayson Minard's article "Blueprint for PHP Applications:
Bootstrapping (Part 2) ". I suggest you read the entire series of articles.

Startup File: index. php
Zf-tutorial/index. php is our Startup File, starting with the following code:
Zf-tutorial/index. php

Note that we didn't put it at the end of the file?>, Because it is not necessary and has the following advantages: We can prevent unnecessary whitespace from occurring in?> The following error is difficult to debug.

Let's repeat this file

QUOTE:

Error_reporting (e_all | e_strict );
Date_default_timezone_set ('Europe/London ');

The first line ensures that we can see the error we made (assuming that display_errors in php. ini is set to on), PHP5.1 + requires setting the current time zone. Obviously, you should select your own time zone.

QUOTE:

Set_include_path ('.'. path_separator. './library'
. Path_separator. './application/models /'
. Path_separator. get_include_path ());
Include "Zend/loader. php ";

Zend Framework is designed in this way. All files must be included in the include path. We also include our model directory in the include path so that we can easily load our model classes in the future. At the beginning, we must include Zend/Loader. php so that we can access the Zend_Loader class. There is a static method in the Zend_Loader class so that we can load other Zend Framework classes, for example:
Zend_Loader: loadClass ('zend _ Controller_Front ');

Zend_loader: loadclass loads named classes. It converts the underline into a path separator and adds the. php suffix at the end. In this way, the class zend_controller_front will be loaded from Zend/controller/font. php. If you use the same naming rules in your class library, you can use zend_loader: loadcass () to load them. We need to load the Controller class and the routing class.

The front-end controller uses the routing class to map the request URL to the correct PHP function, and then displays the page. To make routing work, we need to solve which part of the URL is directed to the index. php path, so that it can find the URL element after that point. This is done by the request object. It performs a great job in automatically detecting the correct base URL, but if it does not work on your settings, you can use the $ frontcontroller-> setbaseurl () function to override.

We need to configure the front-end router so that it can find out the controller from which directory.

Quote:

$ Frontcontroller = zend_controller_front: getinstance ();
$ Frontcontroller-> setcontrollerdirectory ('./application/controllers ');
$ Frontcontroller-> throwexceptions (true );

This is a tutorial and we plan to run a test system. I decided to let the front-end controller throw all exceptions. By default, the front-end controllers capture them and store them in the _ exceptions attribute created by the response object. The response object obtains all information about the response to the URL. This includes the HTTP header, page content, and exceptions. The front-end controller will automatically send the header and display the page content before it completes its work.

This is a bit confusing for new users who want to learn the Zend framework. It is easy to throw a new one, so that exceptions are easily visible. Of course, on a production server, you should not Display error messages to users!

Finally, we have reached the core of the problem and can run our program:

Quote:

// Run!
$ Frontcontroller-> dispatch ();

If you use http: // localhost/zf_tutorial/for testing, the fatal error is similar:

QUOTE:

Fatal error: uncaught exception 'zend _ controller_dispatcher_exception'
Message 'invalid controller specified (INDEX) 'in...

This tells us that our program has not been set. Before we start, we 'd better discuss what we are going to do.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.