Step by step to build your own lightweight mvcphp framework-(iv) a domestic lightweight framework Amysql Source Analysis (3) Total Process Object

Source: Internet
Author: User

Amysqlprocess class, the total process object of the framework./amysql/amysql.php

The following is still the same as before, first on the code ~

classamysqlprocess { Public $AmysqlController;  Public $ControllerName;  Public $ActionName;  Public $ControllerFile; functionProcessstart () {Global $Config; if($Config[' Httppath '])        {            $GETParam= (strlen($_server[' Request_uri ']) >strlen($_server[' Script_name '])) ?Explode(‘/‘,Trim(Str_ireplace($_server[' Script_name '], ',$_server[' Request_uri ']), '/'): '; $GETCount=Count($GETParam); if($GETCount> 1)                 for($i= 2;$i<$GETCount; ++$i)$_get[$GETParam[$i]] =isset($GETParam[++$i]) ?$GETParam[$i] : ‘‘; }        $magic _quotes=function_exists(' GET_MAGIC_QUOTES_GPC ')?GET_MAGIC_QUOTES_GPC() :false;//whether the environment has a filter        if($Config[' Filter '])//Turn on filtering        {            ( !$magic _quotes&& (Amysql::filter ($_get, ' addslashes ') && amysql::filter ($_post, ' addslashes ') && amysql::filter ($_cookie, ' addslashes ') && amysql::filter ($_files, ' Addslashes ')) ); }        Else        {                ( $magic _quotes&& (Amysql::filter ($_get, ' stripslashes ') && amysql::filter ($_post, ' stripslashes ') && amysql::filter ($_cookie, ' stripslashes ') && amysql::filter ($_files, ' Stripslashes ')) ); }        $thisControllername =!Empty($GETParam[0])?$GETParam[0]: ((isset($_get[$Config[' Urlcontrollername ']) &&!Empty($_get[$Config[' Urlcontrollername '])) ?$_get[$Config[' Urlcontrollername '] : ' Index '); $this-Controllername =Str_replace(_pathtag, Directory_separator,$this-controllername); $thisActionName =!Empty($GETParam[1])?$GETParam[1]: ((isset($_get[$Config[' Urlactionname ']) &&!Empty($_get[$Config[' Urlactionname '])) ?$_get[$Config[' Urlactionname '] : ' Indexaction '); if(!Preg_match('/^[a-za-z] (\w) *$/',$thisControllername) | | !Preg_match('/^[a-za-z] (\w) *$/',$this-actionname)) Amysql:: Amysqlnotice (' Illegal routing address! ')); $this-Controllerfile = _controller.$thisControllername. '. php '; }    functionControllerstart () {if(!In_array($thisActionName,Get_class_methods($this-Controllername),true)) Amysql:: Amysqlnotice ($thisActionName. ' method does not exist '); Define("Controllername",$this-controllername); Define("ActionName",$this-actionname); $this-Amysqlcontroller =New $this->controllername ($_get);//Instance Controller        $this-Amysqlcontroller, {$thisActionName} ();//Execution Method    }}

First of all, here's how this processstart () process starts

The first sentence in this method is to introduce a global variable $config, which is an array that stores the initial configuration of many frameworks, and can be viewed here in detail.

This is followed by the use of $config[' Httppath '] to determine whether to open index.php/controller/action/name/value such a URL pattern, which is also said in the config configuration file.

If this URL pattern is started, then the URL is disassembled, and then the content is put into the $_get global variable.

Now for $_server[' Request_uri ' and $_server[' Script_name '] for a detailed explanation

REQUEST_URI 用来指定要访问的页面,包含后面的参数。

Script_name contains the path to the current script, without parameters.

The parameter here refers to the URL address? The latter part

For example, the following lines of code

<?  Echo ' Request_uri: '. $_server [' Request_uri ']. " ; Echo ' Script_name: '. $_server [' Script_name '];

I visit http://127.0.0.1/cs/uri.php/aaa/bbb it outputs the result is

The following is a detailed analysis of the meaning of this sentence

(strlen($_serverstrlen($_serverexplodetrim(str_ Ireplace($_server$_server[' Request_uri ']), '/'): ';

The subject of this sentence is a ternary operator, conditional on strlen ($_server[' Request_uri ') > strlen ($_server[' script_name ')), This means that when the URL address contains parameters, the parameter is split, or a space is returned.

To split the parameters, first use the Str_ireplace bar URL address of the Script_name this part to remove, leaving only the parameters section.

For example above, now use the following line of code to output a

<?  echostr_ireplace($_server$_server[' Request_uri ']);

So there's only a parameter left ~

And trim it is the role of the URL before and after the or does not have a/, such as/aaa/bbb/to convert to AAA/BBB

The explode function is then used to split the string by "/" and into the array $getparam.

The reason behind the for loop is starting from 2 is because $getparam[0] represents the controller, $GETParam [1] represents the method. These are not parameters, they are all in the $_get, and they are some parameter information.

The following controller name and method name is the first detection of the existence of $getparam, if it exists in the $getparam, if it does not exist then the detection according to the controller name and method names defined in config to $_get the Super global variable, if not found then use the default.

Here is the Controllerstart () function, which is used to turn on the controller and to create a new object based on the controller name into $this-Amysqlcontroller.

This created object uses an auto-load magic function __autoload (this function is written to the function.php file: Have not been found at the beginning. /(ㄒoㄒ)/)

function __autoload ($className) {       if(!  File_exists$className . '. php ') amysql::amysqlnotice (' controller file does not exist ');     include_once $className . '. php ';     if (! class_exists ($className))                       Amysql::amysqlnotice (' Controller not present ');}

This is the end of the analysis, and the next section will tell you the contents of the Controller Logic layer (C) Amysqlcontroller class ~

Step by step build your own lightweight mvcphp framework-(iv) a domestic lightweight framework Amysql Source Analysis (3) Total Process object

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.