The example in this article describes the Zend Framework entry-level application. Share it with everyone for your reference, the details are as follows:
.htaccess file
.htaccess file is used to implement URL reset, that is, when the user accesses a resource, it will be redirected to the specified under the file.
Code example:
RewriteEngine on RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
Among them, line 1 means that the reset engine is turned on, and line 2 means that when accessing other than js, ico, gif, jpg, png, css file,
will be reset to the index.php file.
Note:
This rewrite rule can only be used on the Apache server and requires the use of the Apache redirection module. To be effective, the function must be enabled. Confirm that
#LoadModule rewrite_module modules/mod_rewrite.so
under Apache's httpd.conf configuration file is turned on, that is, remove the # sign.
Automatically generate directory
After decompressing the downloaded zend, there will be two core directories, one is library and the other is bin. There is a zf.bat file under bin that can be used to intelligently generate the Zend Framework directory structure.
But certain configuration must be done before use, that is, the configuration of environment variables. Only in this way can its commands be called directly. Otherwise the build command cannot be called.
1. Configure environment variables
The configuration process is as shown in the figure:
After adding the path, you can directly enter the "small Black box", that is, the instruction is directly called in CMD. Two are configured here, one is the bin directory under zend, and the other is the php directory under xampp. Because the instructions need to use some files in these two directories.
2. Whether the test is successful
#The above message appears, indicating that the environment variable configuration is successful.
3. Execute the generation command
zf create project zfdemo
At this time, a project directory will be generated, as shown in the above prompt. There will be a directory zfdemo under the C drive.
4. Copy the information in the directory to the project folder and test
The entry file here is under public
So in Enter the address "http://localhost/zendDemo/public/"
Note: The name of the project is zendDemo
If nothing goes wrong, you will see the successful execution result. . As shown in the figure below:
#Note: Because I rewrote the default view file content, all I saw were the four words "Hello World".
Transformation
1. Create your own controller
Create a controller named SelfController.php in the application/controllers/ directory and enter the following code.
<?php class SelfController extends Zend_Controller_Action{ public function selfAction(){ //self方法 } public function myAction(){ //my方法 } }
The above code defines a SelfController class, and defines the selfAction() method and myAction() method in this class.
2. Create the corresponding view file
Create the self.phtml file in the application/views/scripts/self/ directory, the code is as follows:
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> t tle>self方法的显示视图</title> </he > <h1> self方法的显示视图</h1> </body
Continue to create the my.phtml file in the same directory, the code is as follows:
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>my方法的显示视图</title> </head> <body> <h1>my方法的显示视图</h1> </body> </html>
3. Testing and Execution
You should first understand how Zend Framework handles HTTP requests. By default, the first part of the URL is mapped to a controller, and the second part is mapped to the Action method in the controller class.
The execution result is shown in the figure below:
I hope this article will be helpful to everyone’s PHP program design based on the Zend Framework framework.
For more detailed Zend Framework introductory application examples and related articles, please pay attention to the PHP Chinese website!