PHPMVC mode development (1): Unified site Portal

Source: Internet
Author: User
This article describes how to use php to implement MVC-mode development files. Technical articles on the MVC mode can be used everywhere on the Internet, so this document will not talk about the advantages and disadvantages of this mode (in fact, I am not clear), but only about its php technology implementation. In the future series of articles, we will also focus on technology. 1. implement unified website SyntaxHighlighter. all (). This article describes how to use php to implement MVC-mode development files. Technical articles on the MVC mode can be used everywhere on the Internet, so this document will not talk about the advantages and disadvantages of this mode (in fact, I am not clear), but only about its php technology implementation. In the future series of articles, we will also focus on technology.

1. implement a unified website portal (call the Controler layer method in MVC, that is, the control layer)

You may often see this path on the Internet (http://www.aaa.com/aaa/bbb/aaa? Id = 5), confusing. There are several possibilities for implementing such a website:
1. hiding the file extension has different opinions on the advantages of this approach, but I personally think it is unnecessary;
2. use website redirection rules to implement virtual paths;
3. implement virtual paths by Force file resolution.
You can use 23rd methods to achieve unified website interfaces, integrate websites reasonably, and better reflect website security and architecture, most websites using these two methods are built and implemented using the "MVC" mode.
The following is an example.

The access path is as follows:
.../Test/******/Bad
.../Test/******/Good
(The "*******" can be replaced with any string. "..." is your web path)
The directory structure of the file is as follows:
| --. Htaccess
| -- Test
| -- Application. php
| -- Controler/GoodControler. php
| -- Controler/BadControler. php

Note that the file ". htaccess" cannot be directly created in windows. it can be created in command line mode.
File 0 :(. htaccess) (This file is used to change the apache configuration method)

Forcetype application/x-httpd-php


File test. php
/*-------------------------------------
* Test. php
*
* Files used as the portal for your website
* Used for initialization and entry
* Call and execute the call of Controler
*
-------------------------------------*/
Require "Application. php ";
$ Aa = new Application ();
$ Aa-> parse ();
$ Aa-> go ();

?>
 
File GoodControler. php
/*-------------------------------------
* GoodControler. php
*
* Used to control access from url =/test/Good
*
-------------------------------------*/
Class GoodControler {
/*
* Control Class Call method, the only interface reported to the external
*/
Function control (){
Echo "this is from GoodControler url = *********/test/Good ";
}
}

?>
 
File BadControler. php
/*-------------------------------------
* BadControler. php
*
* Used to control access from url =/test/Bad
*
-------------------------------------*/
Class BadControler {
/*
* Control Class Call method, the only interface reported to the external
*/
Function control (){
Echo "this is from GoodControler url = *********/test/Bad ";
}
}

?>

File Application. php
/*-------------------------------------
* Application. php
*
* It is used to implement a unified portal for websites and call the Controler class.
*
-------------------------------------*/
Class Application {
// Used to record the operation to be performed
Var $ action;
// Name of the controler file
Var $ controlerFile;
// Controler class name
Var $ controlerClass;

Function Application (){
}

Function parse (){
$ This-> _ parsePath ();
$ This-> _ getControlerFile ();
$ This-> _ getControlerClassname ();
}
/*
* Parse the current access path to get the action to be taken.
*/
Function _ parsePath (){
List ($ path, $ param) = explode ("? ", $ _ SERVER [" REQUEST_URI "]);
$ Pos = strrpos ($ path ,"/");
$ This-> action = substr ($ path, $ pos 1 );
}
/*
* Parse the path of the controler file to be used for the $ action through the action $ action.
*/
Function _ getControlerFile (){
$ This-> controlerFile = "./Controler/". $ this-> action. "Controler. php ";
If (! File_exists ($ this-> controlerFile ))
Die ("Controler file name (". $ this-> controlerFile. ") parsing error ");
Require_once $ this-> controlerFile;
}
/*
* Parse the $ action to get the controler class name for this $ action.
*/
Function _ getControlerClassname (){
$ This-> controlerClass = $ this-> action. "Controler ";
If (! Class_exists ($ this-> controlerClass ))
Die ("Controler class name (". $ this-> controlerClass. ") parsing error ");
}
/*
* Call controler to execute the controler action.
*/
Function go (){
$ C = new $ this-> controlerClass ();
$ C-> control ();
}
}
?>
Next we will continue to explain the MVC mode development in PHP.

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.