Step into Zend Framework framework programming (3): run the first program

Source: Internet
Author: User
Tags zend framework

The Framework was invented to improve development efficiency and support development by multiple teams. Traditional flat development always keeps every software project from scratch, and programmers have to repeat the work of "making wheels", while the interface (HTML + CSS + JavaScript) the combination of program logic makes it difficult for programmers to separate the role of interface creation, which may make the application become unable to be maintained in its life cycle.
ZF divides the program code into three different parts (usually independent files) to improve the maintainability of the application ):
Model (m): Application Model section. Focus on the details of the displayed data. Models usually focus on business logic and how to access data from databases. The specific code is represented as a different class, which can be seen as a user-defined class library.
View (v): The view focuses on the content displayed by the user, which is usually HTML.
Controller (c): The Controller combines the model and view to ensure that the correct data is displayed on the page.
That is to say, Zend framework uses the Model-View-controller (MVC) architecture. It separates different parts of the program, making it easier to develop and maintain the application.
1. ZF framework design ideas and strategies
ZF adopts the front-end controller design mode, which sends all users' requests to a central control point. The specific method is that all requests need to be accessed through index. php. Then, the user requests are distributed to the corresponding controller through the script in index. php. The Controller is responsible for calling the logic in the model and presenting the user interface.
2. Apache's support for ZF
To implement ZF's design philosophy, we need the support of web servers. The Apache server is implemented through its mod_rewrite function.
2.1 to configure the mod_rewrite extension, remove the annotator # Before the following statement in the Apache configuration file httpd. conf to make the configuration take effect, that is, load the mod_rewrite.so module.
Loadmodule rewrite_module modules/mod_rewrite.so
Make sure that Apache is configured to support the. htaccess file mode. Normally, you can set
<Directory "C:/program files/Apache Software Foundation/apache2.2/htdocs">
Options indexes followsymlinks
# AllowOverride none
AllowOverride all
Order allow, deny
Allow from all
</Directory>
Change AllowOverride none in to AllowOverride all. If mod_rewrite and. htaccess are not correctly configured, no pages except the home page are displayed.
Save the configuration and restart the apache service for the configuration to take effect.
2.2. htaccess file content:
Rewriteengine on
Rewriterule! /. (JS | ICO | GIF | JPG | PNG | CSS) $ index. php
This means to enable the rewriteengine and send all requests to images, JS scripts, and CSS except JS, ICO, GIF, JPG, PNG, and CSS to the index. PHP file.
3. directory structure of the first program
Create the following two folders in htdocs: phpchina1.com and app_phpchina1.com.
Create the following folders controllers, models, and views under app_phpchina1.com.
The typical folder structure is as follows:
... Htdocs
App_phpchina1.com
Controllers
Models
Views
Library
Zend
Phpchina1.com
The index. php and. htaccess files are stored in the phpchina1.com folder;
Controllers stores controller files;
Models stores controller files;
Views stores view files;
Note that this is only the folder organization form recommended by ZF, but it is not a required form. In actual projects, you can flexibly adapt to your actual situation. This will be discussed in detail later in this tutorial. If the application is very simple, the models and views folders may not be used, as is the "simplest ZF program" below.
4. The simplest ZF program: Hello phpchina1.com!
The exciting time is now. Let's talk about running our first ZF program.
The content of the index. php file is as follows:
<? PHP
Error_reporting (e_all | e_strict );
// Set the time zone
Date_default_timezone_set ('Asia/Shanghai ');
// Specify the path of the referenced File
Set_include_path ('.'.
Path_separator. '../library /'.
Path_separator. '../app_phpchina.com/models /'.
Path_separator. get_include_path ());
Include "Zend/loader. php ";
Zend_loader: registerautoload ();
$ Fc = zend_controller_front: getinstance ();
$ FC-> setcontrollerdirectory (Array (
"Default" => '../app_phpchina1.com/controllers ',
));
$ FC-> throwexceptions (true );
$ FC-> setparam ('noviewrenderer', true );
$ FC-> setparam ('noerrorhandler', false );
// Start running the program
$ FC-> dispatch ();
?>
Content of indexcontroller. php In the controllers Folder:
<? PHP
Class indexcontroller extends zend_controller_action
{
Function indexaction ()
{
Echo "Hello phpchina1.com! ";
}
Function otheraction ()
{
Echo "this is other action .";
}
}
?>
Run:
Enter http://phpchica1.com: 8080 in the browser address bar and press enter, the browser will display the result:
Hello phpchina1.com!
If you see the above information, congratulations!
The code of the two files will be explained in detail in the next section.

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.