04mvc frame principle (8days) 01

Source: Internet
Author: User

Object-oriented keywords:

Class

Public,protected,private

Static

Const

?

Self,parent,static

?

Extends

Implements

Abstract

Interface

Final

?

New

Clone

?

$this

?

?

Introduction to project development

Installing Ecshop

After unpacking the package, get all the files in the upload subdirectory and place it in a directory that can be requested by the browser (renamed)

Request, index.php can!

will automatically jump to the installation page (or you can request install/index.php)

?

Configure PHP

Ecshop requires MySQL extension with GD extension (image processing extension) support!

Configuring the System

Configure Database Information

Configuring information for Ecshop Administrators

?

Other configurations:

Recommended installation test data

Installation can

?

Installation succeeded:

Front Office Address:

?

Day After Tomorrow address:

?

?

Development cycle of the project

Demand analysis

Design

Coding design, how should be implemented!

Two main directions:

    1. Two times development, based on existing products, do post-custom processing! Products, has realized the main business logic function code!

Mall: Ecshop,ecmall,shpex,zen-cart,es-commerce

CMS, content management, blogs, Wordpress,dedecms,drupal,

Forum, Discuz,phpwind

?

    1. Framework-based development

Framework, only the underlying public code, and without the business logic of the product, is the framework!

thinkphp,yii,zendframework,symfony,ci,fleaphp,quephp,

?

?

Encoding implementation

?

Test, Debug, modify

?

On-line, production stage

?

Maintenance, upgrade, revision

?

The evolutionary process of code

One, mixed

Embedded scripting language PHP

How HTML and PHP are encoded

?

The display is separated from the logical phase

?

Finally, you need to put the results of the display and logic together!

You need to load the HTML code in the PHP page!

?

At this point, be sure to request the PHP code that is responsible for the logic:

You need to iterate through the data in the HTML code and show the data!

?

After the code division, easy to maintain and manage! From the point of view of the management project!

?

?

Template file

Note that the current HTML page appears: The Display format section is played by the HTML code, and the data part needs to be implemented by PHP. Data, especially the presentation of data. (Data acquisition is actually responsible for logical PHP code completion)

Such an HTML file is called a modulo (MU) board file (template)

?

Restrict user access to PHP logic files

There are two typical ways of doing this:

1,.htaccess

Deny from all

Using Apache's access control, a directory is set to prohibit access. Put all the templates in the directory!

Control access to this directory:

Note that htaccess comes into effect if Apache opens allowoverride to the directory:

?

?

2, will not allow users to access files, including template files, are placed outside the document root directory!

Browser requests only see files under the document root directory.

?

?

Three, processing data (code that processes business logic), separating from PHP code

Scene, in completing one other page, using the same function on other pages!

Duplication of code that appears in the data manipulation logic!

Resolution: will be re-extracted!

?

New PHP code for data processing

?

?

?

?

Call this function when you need a match list!

?

The evolutionary process of one,two,three:

?

?

MVC Project Layering, design ideas

MVC, the layered idea of a project, refers to the completion of a business logic that requires three major parts, namely:

    1. The specific business logic realizes the part of data manipulation, called M,model, model !
    2. Specific display style implementation part, HTML+CSS+JS, called v,view, view!
    3. The part responsible for the overall process control, which calls M and V. Become a C,controller, controller!

?

?

The implementation of reference four

If the project is implemented according to the above division of labor, the project is designed according to the MVC design idea!

(Just a project layering idea, not deciding on grammar)

?

?

Typical, MVC implementations:

User browser to request controller

Control call model for business logic data processing

The controller calls the view and uses the template to show the data!

?

?

?

Mvc==thinkphp,zend-framework

MVC idea,

The MVC framework, which is based on the MVC design idea, is called the MVC Framework!

?

Processing of the model

Model class

The processing of business logic is finally done through the operation database!

In the MVC hierarchy, the typical model is:

Each model, is an Object! A model object that is responsible for completing the operation of a table in the database!

?

At this point, for the operation of a table, it should be encapsulated in the corresponding model class!

For example, the operation, the match table, will have the following model:

Complete the contest list, and when you get the data, you should call the method that gets the data by instantiating the object of the model Class!

?

Use this model class:

Instantiate the class object and call the method:

?

Anti-Third: Class list

One, controller

Add a controller file and finish issuing commands to the model and view

?

    1. Model

Create a Classmodel model class file

and add a GetList method that returns all the class data

    1. Add a View template

?

Base Model class

Extraction of the public operations of the model

Add a model base class:

All models should inherit from the underlying class of the model:

Do not forget to load the model class in order to:

?

?

The connection to the database is completed when the model object is instantiated!

In the underlying model class, add the constructor method that the quilt class inherits. You need to complete the connection of the data in the construction method

Model.class.php

?

At this point, once the instantiation has completed the connection work

?

Model classes manipulate data using MYSQLDB class objects

?

At this point, in the underlying model class, initializing the database connection is actually instantiating the object of the MySQLdb class and saving it to the $db property of the current Object!

Model.class.php

?

In a specific model, if you need to execute SQL, you should use the MySQLdb class object stored in the Model object $db property to complete:

ClassModel.class.php

?

?

Handling of the Controller

Scene:

Added a class add function ( delete , modify), how to complete

Delete, controller, model, view

Controller:

class_delete_controller.php

?

Model

In Classmodel, you can add methods to

?

View

No need!

?

?

Add a collection of modules, controller code

?

You can't add a controller file to each operation, so how do you manage those operations?

Use the concept of modules!

Modules, module, a collection of similar operations, for example, class management module!

?

Add Class Management module files:

A module file, is a collection of multiple functions!

At this time, complete the class operation, all must request, class_module.php this file!

The question is, how do you decide which specific work the current request should take?

When you request the module file, you should add a parameter that indicates what the current user's browser wants to do!

A (action), action! An action is called an action!

Class_module.php?a=list List

Class_module.php?a=del Delete

?

Typically, you should take advantage of process branching controls:

class_module.php

?

At this point, when the browser requests: you need:

?

?

?

?

?

?

?

?

?

Controller class

Upgrade the Controller module file just completed to a controller class

?

Each function within the module becomes a method of the Controller module class:

ClassController.class.php

?

?

?

At this point, you need to instantiate the Controller class, and you need to determine which method to call!

Need to find an extra file to instantiate the Classcontroller class!

New file:

index.php

?

At this point, I need to see the list:

?

?

?

Entry file, index.php

Because all the controllers are Class!

All requests need to start from index.php and are therefore called entry files for the project!

?

The primary responsibility is to receive the request and, based on the request parameters, distribute the request to a specific method action on a controller. So the entry file is also called: the request Dispatcher !

?

As a function of the overall process control a part, is also the controller. So also known as: Front Controller

?

If you have more than one controller class, you need to instantiate the Controller class with multiple operations!

?

How do you work with dynamic control class instantiation in a file?

Similar to the difference in action action, increase the request parameter, representing the currently requested Controller Class!

C,controller that represents the currently requested control class

For example
Class List: Index.php?c=class&a=list

Student Delete: Index.php?c=student&a=del

?

Therefore, when instantiating a controller class, it is necessary to determine the current C parameter before determining:

?

?

?

Attention:

Once the above plan has been completed:

Request, the user all requests, must pass through the index.php entrance file, distributes to the Controller the action to carry on again!

Index.php?c=xxx&a=yyy

?

Preview

The concept of a framework. Extract the shared code!

Homework

Use six version, to achieve competition, students, class information display!

04mvc frame principle (8days) 01

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.