In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the sample analysis of the introduction to Zend Framework development, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
The details are as follows:
Zend Framework has released! Although it is still in the early stages of development, this tutorial highlights some of the best features and guides you through the construction of a simple program.
Zend first released ZF in the community. Based on the same idea, this tutorial is written to demonstrate the existing functionality of ZF. Since this tutorial is published online, I will update it when the ZF changes to make it as effective as possible.
Request
Zend Framework requires PHP5. To make better use of the code in this tutorial, you also need an Apache web server. Because the demonstration program (a news management system) uses mod_rewrite.
The code for this tutorial is free to download, so you can try it yourself. You can download the code: http://brainbulb.com/zend-framework-tutorial.tar.gz from Brain Buld's website.
Download ZF
When you start this tutorial, you need to download the latest version of ZF. You can manually select a tar.gz or zip file from http://framework.zend.com/download to download with your browser, or use the following command:
$wget http://framework.zend.com/download/tgz$ tar-xvzf ZendFramework-0.1.2.tar.gz
Tip: Zend plans to provide its own PEAR channel for simplified download.
Once you have downloaded the preview, put the library directory in a convenient place. In this tutorial, I renamed library to lib to have a concise directory structure:
App/
Views/
Controllers/
Www/
.htaccess
Index.php
Lib/
The www directory is the document root, the controllers and views directories are empty directories that will be used later, and the lib directory is from the preview version you downloaded.
Start
The first component I want to introduce is Zend_Controller. In many ways, it provides the foundation for the program you develop, and partly determines that Zend Framework is more than just a collection of components. However, you need to put all the requests you get into a simple PHP script before using it. This tutorial uses mod_rewrite.
Using mod_rewrite is an art in itself, but fortunately, this particular task is particularly simple. If you are not familiar with the general configuration of mod_rewrite or Apache, create an .htaccess file in the document root and add the following:
RewriteEngine onRewriteRule! /. (js | ico | gif | jpg | png | css) $index.php
Tip: one of Zend_Controller 's TODO projects is to unrely on mod_rewrite. To provide a preview version of the example, this tutorial uses mod_rewrite.
If you add this content directly to httpd.conf, you must restart the web server. But if you use .htaccess files, you don't have to do anything. You can put some specific text into index.php and visit any path such as / foo/bar to do a quick test. If your domain name is example.org, visit http://example.org/foo/bar.
You also need to set the path to the ZF library to include_path. You can set it in php.ini, or you can put the following directly in your .htaccess file:
Php_value include_path "/ path/to/lib"
Zend
The Zend class contains a collection of static methods that are often used. Here is the only class you want to add manually:
Once you have included Zend.php, you have included all the class methods of the Zend class. You can simply load other classes with loadClass (). For example, load the Zend_Controller_Front class:
Include_path understands the organization and directory structure of loadclass () and ZF. I use it to load all other classes.
Zend_Controller
Using this controller is very intuitive. In fact, I didn't use its rich documentation when I wrote this tutorial.
Tip: the document is currently available on http://framework.zend.com/manual/zend.controller.html.
I started with a front controller called Zend_Controller_Front. To understand how it works, put the following code in your index.php file:
If you prefer object links, you can use the following code instead:
Now if you visit / foo/bar, an error will occur. That's right! It lets you know what's going on. The main problem is that the IndexController.php file cannot be found.
Before you create this file, you should understand how ZF wants you to organize these things. ZF splits the access request. If you are accessing / foo/bar, foo is controller and bar is action. Their default values are all index.
If foo is controller,ZF, it will look for the FooController.php file in the controllers directory. Because this file does not exist, ZF is returned to IndexController.php. As a result, it was not found, so the report was wrong.
Next, create an IndexController.php file in the controllers directory (which can be set with setControllerDirectory ()):
As just explained, the IndexController class handles requests from index controller or controller that do not exist. The indexAction () method handles access with action as index. Keep in mind that index is the default value for controller and action. If you visit /, / index or / index/index,indexAction () the method will be executed. (the last slash does not change this behavior.) Accessing any other resource will only lead to an error.
Before you continue, add another useful class method to IndexController. Whenever you access a controller that does not exist, the noRouteAction () class method is called. For example, if FooController.php does not exist, accessing / foo/bar executes noRouteAction (). However, there are still errors in accessing / index/foo, because foo is action, not controller.
Add noRouteAction () to IndexController.php:
In the example, $this- > _ redirect ('/') is used to describe the behavior that may occur when noRouteAction () is executed. This redirects access to the non-existent controllers to the root document (home page).
Now create the FooController.php:
If you visit / foo/bar again, you will find that barAction () is executed because bar is action. Now not only do you support friendly URL, but you can be so organized in just a few lines of code. Cool!
You can also create a _ _ call () class method to handle undefined action like / foo/baz.
Now you only need a few lines of code to handle user access, and you're ready to continue.
Zend_View
Zend_View is a class that helps you organize your view logic. This is agnostic to the template-system, and for simplicity, templates are not used in this tutorial. You might as well use it if you like it.
Keep in mind that all visits are now handled by front controller. Therefore, the application framework already exists, and it must be followed. To demonstrate a basic application of Zend_View, modify IndexController.php as follows:
Create an example.php file in the views directory:
This Is an Example
This is an example.
Now, if you visit the root resource of your website, you will see the content of example.php. This is still useless, but you need to know that you are developing web applications in a way that is very clearly structured and organized.
To make the Zend_View application clearer, modify your template (example.php) to include the following:
Two features have been added. The $this- > escape () class method is used for all output. Even if you create your own output, just like this example. It's also a good habit to avoid all output, which helps you prevent cross-site scripting attacks (XSS) by default.
The $this- > title and $this- > body attributes are used to display dynamic data. These can also be defined in controller, so we modify the IndexController.php to specify them:
Now that you visit the root directory again, you should be able to see the values used by the template. Because the $this you use in the template is the instance executed within the scope of Zend_View.
Keep in mind that example.php is just a normal PHP script, so you can do whatever you want. Only efforts should be made to use templates only when required to display data. Your controller (controller distributed module) should handle all your business logic.
Before I continue, I'd like to make one last tip about Zend_View. Initializing the $view object within each class method in controller requires some extra input, and our main goal is to make it easier to quickly develop web applications. If all templates are placed in one directory, it is debatable whether to call setScriptPath () in every example.
Fortunately, the Zend class contains a register to help reduce the workload. You can use the register () method to store your $view object in a register:
Search with the registry () method:
Based on this, this tutorial uses registers.
Zend_InputFilter
The last component discussed in this tutorial is Zend_InputFilter. This class provides a simple and efficient input filtering method. You can initialize by providing a set of data to be filtered.
This will set ($_ POST) to NULL, so you can't enter it directly. Zend_InputFilter provides a simple, centralized set of class methods that filter data based on specific rules. For example, you can use getAlpha () to get the letters in $_ POST ['name']:
The parameters of each class method are keywords corresponding to the element to be filtered. Objects ($filterPost in the example) can protect the data from tampering and have better control over the operation and consistency of the data. Therefore, when you manipulate input data, you should always use Zend_InputFilter.
Tip: Zend_Filter provides the same static methods as the Zend_InputFilter method.
Construction of news management system
Although the preview provides many components (and even many have been developed), we have discussed all the components needed to build a simple program. Here, you will have a clearer understanding of the basic structure and design of ZF.
Everyone develops different programs, and Zend Framework tries to accommodate these differences. Similarly, this tutorial is written according to my preferences, please adjust it according to your own preferences.
When I develop a program, I do the interface first. This doesn't mean I spend all my time on tags, stylesheets and pictures, but I think about it from a user's point of view. So I think of the program as a collection of pages, each of which is a separate URL. The news system is made up of the following web sites:
/
/ add/news
/ add/comment
/ admin
/ admin/approve
/ view/ {id}
You can directly link these sites to controller. IndexController lists news, AddController adds news and comments, AdminController handles management such as approved news, and the display of ViewController specific news and corresponding comments.
If your FooController.php is still there, delete it. Modify the IndexController.php to add the appropriate action and some comments for the business logic:
Next, create the AddController.php file:
Remember that the indexAction () method of AddController cannot be called. This class method is executed when / add is accessed. Because users can access the URL manually, which is possible, you need to redirect users to the home page, display errors, or do what you think is appropriate.
Next, create the AdminController.php file:
Finally, create the ViewController.php file:
Like AddController, the index () method cannot be called, so you can use action as you see fit. ViewController is a little different from the others because you don't know what an effective action is. To support URLs like / view/23, you need to use _ _ call () to support dynamic action.
Database operation
Because the database component of Zend Framework is not yet stable, I hope this demonstration can be made a little easier. I used a simple class that uses SQLite to store and query news items and comments.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.