PHP MVC pattern Development (i): Unified Site Portal _php Tutorial

Source: Internet
Author: User
Tags parse error
This article describes how to implement the MVC pattern development file using PHP. Technical articles on the MVC pattern are available anywhere on the Web, so this document will no longer tell you the pros and cons of this pattern (which I'm actually not sure about), but only the implementation of his PHP technology. And in the future series of articles is also based on technology-based.

first, to achieve a unified site portal (in MVC call the Controler layer method, that is, the control layer)

People may often see such a path on the Internet (http://www.aaa.com/aaa/bbb/aaa?id=5), confusing, such a way to achieve the site has several possibilities:
1, the extension of hidden files, the benefits of this approach, there are divergent opinions, but the individual feel that there is no need;
2, using the site redirection rules, the realization of virtual path;
3, the method of forcing file parsing, the realization of virtual path.
The 23rd method can achieve the unified interface of the website, reasonable integration of the site, better reflect the security and architecture of the site, using the two methods of the site is mostly using the "MVC" mode of construction and implementation.
Here is an example

The access path is as follows:
.../test/*******/bad
.../test/*******/good
(where "******" 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 the file ". htaccess", which cannot be created directly under Windows, can be established in command-line mode.
File 0: (. htaccess) (this file is used to change the way Apache is configured)

Forcetype application/x-httpd-php


File test.php
/*-------------------------------------
* test.php
*
* As the entry file for your website
* Used for initialization and entry
* Call to execute controler
*
-------------------------------------*/
Require "application.php";
$AA = new Application ();
$aa->parse ();
$aa->go ();

?>

File goodcontroler.php
/*-------------------------------------
* goodcontroler.php
*
* Used to control the access of Url=/test/good
*
-------------------------------------*/
Class goodcontroler{
/*
* Control class Invocation method, unique report to external interface
*/
function control () {
echo "This was from Goodcontroler Url=*********/test/good";
}
}

?>

File badcontroler.php
/*-------------------------------------
* badcontroler.php
*
* Used to control the access of Url=/test/bad
*
-------------------------------------*/
Class badcontroler{
/*
* Control class Invocation method, unique report to external interface
*/
function control () {
echo "This was from Goodcontroler Url=*********/test/bad";
}
}

?>

File application.php
/*-------------------------------------
* application.php
*
* Used to implement the unified portal of the website, call the Controler class
*
-------------------------------------*/
Class application{
Used to record what you want to do
var $action;
Path name of the Controler file
var $controlerFile;
Class name of Controler
var $controlerClass;

function application () {
}

Function Parse () {
$this->_parsepath ();
$this->_getcontrolerfile ();
$this->_getcontrolerclassname ();
}
/*
* Parse the current access path to get the action
*/
function _parsepath () {
List ($path, $param) = Explode ("?", $_server["Request_uri"]);
$pos = Strrpos ($path, "/");
$this->action = substr ($path, $pos 1);
}
/*
* Through the action $action, the path of the Controler file to be used by the $action is resolved.
*/
function _getcontrolerfile () {
$this->controlerfile = "./controler/". $this->action. " Controler.php ";
if (!file_exists ($this->controlerfile))
Die ("Controler file name (". $this->controlerfile. ") Parse error ");
Require_once $this->controlerfile;
}
/*
* The Controler class name to be used by the $action is parsed by the action $action.
*/
function _getcontrolerclassname () {
$this->controlerclass = $this->action. " Controler ";
if (!class_exists ($this->controlerclass))
Die ("Controler Class name (". $this->controlerclass. ") Parse error ");
}
/*
* Call Controler, perform controler action
*/
function Go () {
$c = new $this->controlerclass ();
$c->control ();
}
}
?>
the next section goes on to explain the MVC pattern development in PHP.

http://www.bkjia.com/PHPjc/486163.html www.bkjia.com true http://www.bkjia.com/PHPjc/486163.html techarticle This article describes how to implement the MVC pattern development file using PHP. Technical articles about the MVC pattern can be anywhere on the web, so this document will no longer tell the pros and cons of this pattern (actually ... ).

  • 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.