Growing Together with Zend framework (2)

Source: Internet
Author: User
Tags zend framework

Connection: Growing Together with Zend Framework (1)

Website
We plan to build a very simple inventory system website to manage our CD favorites. On the home page, we will list our favorites and allow us to add, edit, and delete these favorite CDs. We plan to store these in a database. The database is designed as follows:


1. JPG

Required page
The following pages are required.


2. JPG

Organization page
Before setting files, it is important to understand how Zend Framework organizes pages. The page of each application is called "action", and many "actions" constitute a controller. For example, the URL http: // localhost/zftutorial/news/view Controller in this format is news, and action is view. It allows related actions to be grouped. For example, a news controller can have current, archived, and view actions. The Zend Framework MVC system also supports the controller module, but this tutorial is not large enough to have to use it.
The Zend Framework controller regards index as a default action and retains it as a special action. In this way, the indexaction in the news controller is executed for URLs such as http: // localhost/zf-tutorial/news. Zend Framework also retains a default controller, which is also called index.
In this way, http: // localhost/zf-tutorial/will execute the action index under the index controller. As a simple tutorial, we do not intend to involve "complicated" things such as login, it will be another tutorial... ... Because we have four pages for albums, we will organize them into a single controller as four actions. We will use the default controller and four actions, as shown in the following table:


3. JPG

Set Controller
Now you can set the controller. In Zend Framework, a Controller is a class that must be called {Controller name} Controller. Note that {Controller name} must start with an uppercase letter. In addition, this class must be in a file such as {Controller name} Controller. php. This file must also be in a specific Controller directory. Note that {Controller name} must start with an uppercase letter and other letters must be in lowercase. Each action is a public function in the Controller class. The name must be {action name} Action. In
Here, {action name} should start with a lowercase letter.

In this way, in the zf-tutorial/application/controllers/IndexController. php file, our controller class is called IndexController:
Zf-tutorial/application/controllers/IndexController. php

First, let each action output their names and navigate to the URL in the following table for testing:


4. JPG

Now, we have a working router in the program and the correct action that can be executed on each page. If it cannot work, please refer to the "Troubleshooting" section after this tutorial to see if it can help.
Now, let's look at the view.

Set View
The view of Zend Framework is called Zend_View. The view allows us to separate the code of the displayed page from the action function.
The basic Zend_View usage is:
$ Vie

QUOTE:

W = new Zend_View ();
$ View-> setScriptPath ('/path/to/view_files ');
Echo $ view-> render ('view. php ');

If we plan to directly put this skeleton in the action Function and repeat this code that is meaningless to the action, it will be very easy. But we would rather initialize it elsewhere and then use this initialized object in action.

The Zend Framework designer has predicted this type of problem and provided a solution, which is put in Zend_Controller_Action. There are two helper functions: initView () and render (). The initView () function creates a Zend_View object and assigns a value to the $ view attribute. Let's assign a value to it. It also sets the Zend_View object to search for views/scripts/{controller name} so that the view script can be called. The View Script is called Through render (). It will call the script {action_name}. phtml and append it to the body of the response object. The response object is used
Header, body content, and MVC system exceptions are put together. The front-end Controller then automatically sends the header and the body content at the end of the dispatch.

To integrate the view into our application we need to initialise the view in the init () function and then ensure we call render () in each action. we also need to create some view files with test display code.

To integrate the view into the program, we need to initialize the view in the init () function and ensure that render () is called in each action (). We also need to create some view files with test display code.

Modify IndexController as follows. (The modified part is in italics ). As you can see, we add a new function called init (), which is automatically called by the constructor of Zend_Controller_Action. This ensures that the view can be initialized at the beginning and we are sure that it is ready for use in the action function.
Zf-tutorial/application/controllers/IndexController. php

In each function, we assign a title variable to the view attribute, and then call render () to display the view template. Note that the actual display does not happen-it is completed by the front-end Controller after the dispatch process ends.

Now we need to create four view files. These files are known templates, and the render () method expects that each template file is named after its action and has a. phtml extension to indicate that it is a template file. The template file must be in the subdirectory after the controller, so the four files are:
Zf-tutorial/application/views/scripts/index. phtml

Zf-tutorial/application/views/scripts/index/add. phtml

Zf-tutorial/application/views/scripts/index/edit. phtml

Zf-tutorial/application/views/scripts/index/delete. phtml

Test each controller/action, which should display four italic titles

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.