PHP Example Learning ———— MVC architecture Pattern Analysis and design

Source: Internet
Author: User
Tags php example

Blogging, not easy, your comments and reprint, is my motivation,

But please specify the source, the next-door Lao Wang's Development park: Http://www.cnblogs.com/titibili

January 31, 2016 18:53 Ygirl

Model view (view) controller (Controller)

Most applications were previously created through the process mode

However, code aliasing

MVC Benefits:

MVC Development Environment:

Editor: EmEditor

PHP Environment: Using Wampserve integrated Software installation configuration

: http://www.wampserver.com/en/

Instance One

Name the following file:

One creation of the first controller file:

1.A is saved as a file, change the line wrapping mode to only LF (UNIX) (based on LF development), change the encoding method to UTF-8 without signing.

2.a. Create a class (with the class keyword)

b Send show () command via controller

Two create the first model file (method form with Create controller file, type the following code)

Three create the first view file (method form with Create controller file, type the following code)

MVC Run Process

1 views: Invoking the controller, issuing instructions

2. Controller: Select a suitable model according to the instruction

3. Model: Take the corresponding data according to the controller instruction

4. Controller: Select the corresponding view by command

5. View: The data taken from step 3 is displayed according to the user's needs

Four new Create a test.php file

MVC Run Process

1 views: Invoking the controller, issuing instructions

2. Controller: Select a suitable model according to the instruction

3. Model: Take the corresponding data according to the controller instruction

4. Controller: Select the corresponding view by command

5. View: The data taken from step 3 is displayed according to the user's needs

A use require_once () to introduce the three files created above

b Instantiate the controller file (use new to create an object named TestController and assign a value to a variable, that is, the controller is created)

C Execute the controller inside the method

The following completes the first step of the MVC run process

Go back to the controller file (follow the MVC run process in sequence)

A. Controller selection model $testModel =new Testmodel ();

B,. The model obtains the corresponding data $data = $testModel->get (); (assigns the data obtained by the method to the database variable)

C. Controller selection View $testView =new TestView ();

D. Display data $testView->display ($data);

Configure Test URLs

Test results:

Summarize:

The role of the controller is to invoke the model and invoke the view to pass the data produced by the model to the view and have the relevant view go to the display.

The role of the model is to get the data and process the returned data.

The purpose of the view is to organize, beautify, and ultimately output the data obtained to the user terminal.

Directory Specification Reference:

Entry procedures: Also known as single entry mechanism, refers to a Web application, all requests are pointing to a script file (such as a site page is index.php? XXXX form) All access to the use of the program must pass through this portal.

Benefits: Access restrictions require only one file limit;

Makes MVC possible.

(multiple controllers, if each program is instantiated for each controller, make calls, and then use the method of the controller, will make a website use a lot of access to the file to instantiate, maintenance trouble, instead, use a single index.php to call the controller, and then let the controller to call M, v module, maintenance is convenient. )

Example Two

Create a controller call function C

Multiple controller, which contains a variety of methods, given different parameters to call a controller, method can be.

function C ($name, $method) {

Require_once ('/libs/controller/'. $name. ' Controller.class.php ');//introduction of controller files

$testController =new TestController (); instantiation

$testController->show (); How to use

Eval (' $obj = new '. $name. ' Controller (), $obj-$method. ' ();‘);/ /eval () converts the character into an executable PHP statement, $testController replaced with $obj, and test is replaced with '. Name '.

}

Create a model call function m

The method of the model comes with the parameter, which is more flexible to use, and the controller with the reference method, but does not allow its own parameters

function M ($name) {

Require_once ('/libs/model/'. $name. ' Model.class.php ');

Eval (' $obj = new '. $name. ' Model (); ');

return $obj;//Returns the object generated by the instantiation

}

Create a view call function V

function V ($name) {

Require_once ('/libs/view/'. $name. ' View.class.php ');

Eval (' $obj = new '. $name. ' View (); ');

return $obj;

}

Create a portal file index.php

A. Unified portal file-led URL format (e.g. index.php?controller= controller name &method= method name)

B. Use a secure way to receive the controller name and method name passed in the portal file

where the Daddslashes () function is used, defined in the function.php

Escaping illegal characters because the get () method is to be filtered

function Daddslashes ($STR) {

Addslashes () escapes special characters such as single quotes

Determines the current open state, returns true on open, and automatically escapes, using Addslashes () without opening

Return (!GET_MAGIC_QUOTES_GPC ())? Addslashes ($STR): $str;

}

The function.php file is as follows:

testController.class.php file modified to:

Configure test URLs (URL form index.php?controller= Controller name &method= method name)

Test results:

Installing Wampserve

: http://www.wampserver.com/en/

After installation, type localhost on the browser to test

Open WS, left-click on the icon below the right window to start all services; Right button icon, you can set the language.

The WWW directory is the WS default hosting Web Page folder

To customize the site root directory:

Change Directory: Click Apache-httpd.conf,ctrl+f to go to find, enter DocumentRoot, find the original storage directory, change to (such as E;/demo), in the drop down file, find another same directory, the same changes to (such as E;/demo)

Left-click the WS icon to restart all services

Write a PHP file to the new specified storage directory, the localhost/php folder name, test results to successfully change the page folder storage directory

WS Multi-site configuration:

Ws-wamp-bin-apache-apache2.4.9-conf-extra-httpd.vhost

<virtualhost *:80>

ServerAdmin [email protected]

DocumentRoot "C:/apache24/docs/dummy-host2.example.com"

ServerName dummy-host2.example.com

Errorlog "Logs/dummy-host2.example.com-error.log"

Customlog "Logs/dummy-host2.example.com-access.log" common

</VirtualHost>

<virtualhost *:80>

DocumentRoot "f:/wamp/www/demo/test01"

ServerName test01.com

</VirtualHost>

<virtualhost *:80>

DocumentRoot "f:/wamp/www/demo/test02"

ServerName test02.com

</VirtualHost>

/*************************finish!***************************/

Blogging, not easy, your comments and reprint, is my motivation,

But please specify the source, the next-door Lao Wang's Development park: Http://www.cnblogs.com/titibili

I really appreciate it! ~

(more wonderful, attention to the public number: Ape with horse)

PHP Example Learning ———— MVC architecture Pattern Analysis and design

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.