ZendFramework Guide (1)

Source: Internet
Author: User
If you request/foo/bar again, you should see that barAction () is executed, because the requested action is bar. This not only supports friendly URLs, but also supports very good organization with very little code. Cool!

Author: Chris Shiflett
Translation: ShiningRay
We invited Chris Shiflett, a PHP security expert and a contributor to the latest Zend Framework, to write an article about the main features of ZF.
This complete and step-by-step tutorial shows you how to use the framework to write a simple news management system.
Build a unique perspective for practical applications.
Zend Framework has finally opened its secret! Although it is still in the early stages of the development process, this article presents the best part that can be used to readers, and guides you to understand this framework by building a simple application process.
Zend released the framework early on and introduced community operations. This guide only lists the features of the framework today. Because this guide is released online, I will update this article as soon as the framework changes, so as to be consistent as possible.
Zend Framework is required to use PHP 5. To fully utilize the code displayed in this guide, you also need an Apache Web server, because the sample application (a News Management System) uses mod_rewrite.
The code in this guide can be freely downloaded, so you can try it yourself. You can download it from the Brain Bulb website:
Http://brainbulb.com/zend-framework-tutorial.tar.gz.
Before you start to read this guide, you need to download a preview release of the framework. You can view the http://framework.zend.com/download in a browser and select tar.gz or zip file for manual download, you can also use the following command line:
Listing 1
$ Wget http://framework.zend.com/download/tgz
$ Tar-xvzf ZendFramework-0.1.2.tar.gz note Zend has planned to provide a separate PEAR channel for easy download.
After downloading the preview and release version, put the directory of this library in a convenient place. In this tutorial, I renamed the library directory name lib
To provide a simple and clean directory structure:
Listing 2
App/
+-Views/
+-Controllers/
Www/
+-. Htaccess
+-Index. php
The lib/www directory is the document root directory. the controllers and views directories are empty and will be used in the future. The lib directory is from the pre-download
Preview.
Getting started
The first component I want to display to you is Zend_Controller. In many cases, it provides a foundation for the application to be developed, and it is also a part that makes Zend Framework surpass the component set. However, before using it, you need to direct all incoming requests to a PHP script. This tutorial uses mod_rewrite to complete this purpose.
How to make good use of mod_rewrite is indeed an art, but fortunately this special task in this article is very simple. If you
If you are not familiar with mod_rewrite or Apache configurations, you can create a. htaccess file under the root directory of the document.
And add the following command:
Listing 3
RewriteEngine on
RewriteRule! \. (Js | ico | gif | jpg | png | css) $ index. phpZend_Controller one of the current tasks is to remove
Mod_rewrite dependency. To provide an example that can be used in the preview version, mod_rewrite is used in this tutorial.
If you add these commands directly in httpd. conf, you must restart the Web service. However, if you use the. htaccess file,
It's better. You can write something in index. php and request some paths to test it.
For example,/foo/bar. For example, if your host is example.org, request the URL http://example.org/foo/bar.
You may also want to include the path of the Framework Library in include_path. You can configure it directly in php. ini, or run the following command:
Put it in the. htaccess file:
Listing 4
Php_value include_path "/path/to/lib" Zend
The Zend class contains a series of common and useful static methods. This is the only class that needs to be manually included:
Listing 5



Include 'zend. php ';

?>

Once Zend. php is referenced, you can access all methods in the Zend class. Using the loadClass () method, loading other classes also becomes
Simple. For example, load the Zend_Controller_Front class:
Listing 6


Include 'zend. php ';

Zend: loadClass ('zend _ Controller_Front ');

?>
The loadClass () method takes into account include_path and also knows the directory structure of the framework. I will use it to load all
Other classes.
Zend_Controller
The use of this controller is more intuitive. In fact, no official documentation is available when I write this guide!
Now there are official documents on the ZF website.
Zend_Controller_Front is a front-end controller. You can put the following code into index. php to understand it.
How it works:
Listing 7

Include 'zend. php ';

Zend: loadClass ('zend _ Controller_Front ');

$ Controller = Zend_Controller_Front: getInstance ();
$ Controller-> setControllerDirectory ('/path/to/controllers ');
$ Controller-> dispatch ();

?>

If you want to use the object chain method, you can rewrite it as follows:
Listing 8


Include 'zend. php ';

Zend: loadClass ('zend _ Controller_Front ');

$ Controller = Zend_Controller_Front: getInstance ()
-> SetControllerDirectory ('/path/to/controllers ')
-> Dispatch ();
?>

Now, an error occurs when/foo/bar is requested. This is good! It can at least tell you there are actions. Main questions
No IndexController. php is found.
Before creating this file, you 'd better first understand how the framework organizes things. The framework splits a request into several parts.
In this example, the request/foo/bar, foo is the controller, and bar is the action. The default values of both are index.
When foo acts as the controller, the framework first searches for the file FooController. php in the controller directory. This document does not exist.
So the framework looks for IndexController. php. If no error is found, an error is reported.
In the controllers Directory (which can be set by setControllerDirectory (), create
IndexController. php:
Listing 9
 

Zend: loadClass ('zend _ Controller_Action ');

Class IndexController extends Zend_Controller_Action
{
Public function indexAction ()
{
Echo 'indexcontroller: indexAction ()';
}
}

?>

The IndexController class processes the request whose controller is index or the specified controller does not exist, as we mentioned earlier.
The indexAction () method processes the request whose action is index. Remember that no matter the controller or action, the default value is index.
Also said. If you try to request/,/index, or/index, the indexAction () method will be executed (the slash at the end will not
Changes such behavior ). Requests to any other resource may produce errors.
Before proceeding, add a useful method noRouteAction () to IndexController (). Once a controller is requested and
Call the noRouteAction () method if it exists. For example, if FooController. php does not exist in the/foo/bar request
Will execute noRouteAction (). However, the/index/foo request still produces an error because foo is an action, not a control.
.
Add noRouteAction () to IndexController ():
Listing 10


Zend: loadClass ('zend _ Controller_Action ');

Class IndexController extends Zend_Controller_Action
{
Public function indexAction ()
{
Echo 'indexcontroller: indexAction ()';
}

Public function noRouteAction ()
{
$ This-> _ redirect ('/');
}
}

?>

In this example, $ this-> _ redirect ('/') is used to describe the common actions that can appear in noRouteAction. This
You can redirect requests to non-existing controllers to the root document (homepage ).
Create FooController. php now:
Listing 11


Zend: loadClass ('zend _ Controller_Action ');

Class FooController extends Zend_Controller_Action
{
Public function indexAction ()
{
Echo 'foocontroller: indexAction ()';
}

Public function barAction ()
{
Echo 'foocontroller: barAction ()';
}
}
?>
If you request/foo/bar again, you should see that barAction () is executed, because the requested action is bar. This not only supports
A friendly URL. at the same time, very little code can be used for organization. Cool!
You can also create a _ call () method to process undefined action requests, such as/foo/baz:

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.