Start your MVC with PHP (a) Integrate your site portal

Source: Internet
Author: User
Tags implement interface parse error
Site

This is a file that describes how to use PHP to implement MVC pattern development. Technical articles about the MVC pattern can be anywhere on the web, so this document will no longer describe the pros and cons of this model (the actual

On is I am not clear), the son speaks his PHP technology realization. And in the later series of articles are mainly speaking technology.

First, the implementation of a unified Web site portal (in MVC call the Controler layer method, that is, the control layer)


You may often see such a path on the Internet (http://www.aaa.com/aaa/bbb/aaa?id=5), it is puzzling that such a site can be implemented in several ways:
1, hidden file extension, the benefits of this approach, there are divergent views, but personally feel that there is no need;
2, the use of the Web site redirection rules to achieve virtual path;
3, the way to enforce file resolution, the implementation of virtual path.
Using the 2\3 method can achieve the unified interface of the website, reasonable integration of the site, better reflect the security and structure of the site, using both ways of the site is mostly using the "MVC" pattern structure

Built and realized.


Here is an example

The access path is as follows:
.../test/*******/bad
.../test/*******/good
(the "Hu" 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", which cannot be built directly under Windows, can be established in command-line mode.


File 0: (. htaccess) (this file is used to change the configuration of Apache)
<files test>
Forcetype application/x-httpd-php
</files>

Document 1: (test.php)

<?php
/*-------------------------------------
* test.php
*
* File as an entry point for your website
* For initialization and entry
* Call to perform controler calls
*
-------------------------------------*/
Require "application.php";
$AA = new Application ();
$aa->parse ();
$aa->go ();

?>

Document 2: (goodcontroler.php)

<?php
/*-------------------------------------
* goodcontroler.php
*
* Used to control access to Url=/test/good
*
-------------------------------------*/
Class goodcontroler{
/*
* The calling method of the control class, the only leakage to the external interface
*/
function control () {
echo "This are from Goodcontroler Url=*********/test/good";
}
}

?>

Document 3: (badcontroler.php)

<?php
/*-------------------------------------
* badcontroler.php
*
* Used to control access to Url=/test/bad
*
-------------------------------------*/
Class badcontroler{
/*
* The calling method of the control class, the only leakage to the external interface
*/
function control () {
echo "This are from Goodcontroler Url=*********/test/bad";
}
}

?>

Document 4: (application.php)

<?php
/*-------------------------------------
* application.php
*
* Used to implement the unified portal of the site, call the Controler class
*
-------------------------------------*/
Class application{
Used to record the action to be taken
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 ();
}
/*
* Resolves the current access path and gets the action to be performed
*/
function _parsepath () {
List ($path, $param) = Explode ("?", $_server["Request_uri"]);
$pos = Strrpos ($path, "/");
$this->action = substr ($path, $pos + 1);
}
/*
* Through the action $action, parse the $action to use the Controler file path
*/
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;
}
/*
* Through the action $action, resolve to get the $action to use the Controler class name
*/
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 ();
}
}

?>



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.