How PHP frames are built

Source: Internet
Author: User
How PHP frames are made

index.php Main entry file



require "Init.php";
$control = new Controller ();
$control Run ();
?>
------- --------------------------------------------------------------------------------------

init.php file


if (!defined (' isexist '))
?? exit ("Please run the program from the portal file");
header ("Content-type:text/html;charset=utf-8");
??
if (!defined (' Root_path '))
??//Here is a dynamic declaration, ' \ \ ' is an escaped backslash, the default ' \ ' is the escape character
? ? Define (' Root_path ', str_replace (' \ \ ', '/', dirname (__file__)));?
? require Root_path. ' /a/config.php ';
? require Root_path. ' /a/controller.class.php ';
? require Root_path. ' /a/view.class.php ';
? require Root_path. ' /a/model.class.php ';
??
?>
----------------------------------------------------------------------------------------------

config.php file


if (!defined (' isexist '))
?? exit ("Please run the program from the portal file");
? $C = Array (
? ' Url_mode ' =>1,//url mode, 1 for normal mode, 2 for Path_info mode
? ' Default ' = ' welcome ',//defaults to the controller
? ' Default_action ' = ' index '//default method
? );
?>
-----------------------------------------------------------------------------------------------

controller.class.php file

?

class Controller
{
? Public function Run ()
? {
? $this->analysis ();
?//Start parsing URLs to obtain the requested controller and method
? $control = $_get[' con '];
? $action = $_get[' act '];
? $action = Ucfirst ($action);
?//The path of the controller file is constructed here
? $controlFile = Root_path. '/controllers/'. $control. '. class.php ';
if (!file_exists ($controlFile))//If the file does not exist with a hint error, the introduction
? {?
? Exit ("{$control}.class.php controller does not exist
". " Please check: ". $controlFile." Whether there is
");
? }
? include ($controlFile);
? $class = Ucfirst ($control);//capitalize the first letter of each word in the controller name as the class name of the controller
if (!class_exists ($class))//Determine if the class exists, if there is no prompt error
? {
exit ("{$control controller class not defined in}.class.php". $class);
?}
? $instance = new $class ();//Create an instance otherwise
If (!method_exists ($instance, $action))//Determine if the $action method exists in the instance $instance, the error is not present
? {
? Exit ("Method is not present in the $class class:". $action);
?}
? $instance $action ();
?}
?
?
?
? Protected function Analysis ()
? {
?//$GLOBALS [' C '] [' url_mode '];
? global $C;//contains an array of global configurations, defined in the config.ph file, and the global declaration $c is called external
if ($C [' url_mode '] = = 1)
?//If the URL pattern is 1 then get the controller in get, which means the URL address is this [url=http://localhost/index.php?c]http://localhost/index.php?c[/url ]= Controller &a= Method
? {
? $control =!empty ($_get[' con ')) trim ($_get[' con '): ";
? $action =!empty ($_get[' act ') "Trim ($_get[' act ']): ';
?}
? else if ($C [' url_mode '] = = 2)//If 2 then use Path_info mode, which is the URL address?? [Url=http://localhost/index.php/]http://localhost/index.php/[/url] Controller/method/other parameters
? {
if (isset ($_server[' path_info ') )
? {
//$_server[' path_info ']url The path information after the file name in the address, not good understanding, to see the example
?//For example your current URL is [Url=http://www.php100.com/index.php]http://www.php100.com/index.php[/url] then your $_server[' PATH_ INFO '] is empty.
?//But if the URL is [Url=http://www.php100.com/index.php/abc/123]http://www.php100.com/index.php/abc/123[/url]
The value of the current $_server[' path_info '] will be the content after the index.php file name/abc/123/
? $path = Trim ($_server[' path_info '), '/');
? $paths = Explode ('/', $path);
? $control = Array_shift ($paths);
? $action = Array_shift ($paths);
?}
?}
?//This determines whether the value of the controller is empty, if it is empty, use the default
? $_get[' con '] =!empty ($control)? $control: $C [' DEFAULT '];
?//the same as above
? $_get[' act '] =!empty ($action)? $action: $C [' default_action '];
?}
}
?>
--------------------------------------------------------------------------------------------------

welcome.class.php file

?

? class Welcome
? {
?? function Index ()
? ? {
?? Echo ' Welcome to use this CMS system ';
? ?}
?? function Run ()
? ? {
?? echo ' Hello ';
? ?}
? ?
?? function Show ()
? ? {
?? Echo ' method name show ';
? ?}
? }

?>

?

Transferred from: http://blog.sina.com.cn/s/blog_6ec912d80101916t.html

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