Principles of building a PHP Framework

Source: Internet
Author: User
PHP framework production principle index. php main portal File & lt ;? Php? Define (ISEXIST, true );? Requireinit. php ;? $ ControlnewController ();? $ Control-& gt; Run ();? & Gt; ------------------------------------- production principles of the PHP Framework

Index. php main portal file

? Define ('isexist', true );
? Require "init. php ";
? $ Control = new Controller ();
? $ Control-> Run ();
?>
Bytes ---------------------------------------------------------------------------------------------

Init. php file

? If (! Defined ('isexist '))
? ? Exit ("run the program from the entry file ");
? Header ("Content-Type: text/html; charset = utf-8 ");
??
? If (! Defined ('root _ path '))
? ? // Here the dynamic declaration, '\' is the escape 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 ';
??
?>
Bytes ----------------------------------------------------------------------------------------------

Config. php file

? If (! Defined ('isexist '))
? ? Exit ("run the program from the entry file ");
? $ C = array (
? 'URL _ mode' => 1, // url mode. 1 indicates normal MODE, and 2 indicates path_info MODE.
? 'Default' => 'Welcome ', // DEFAULT controller
? 'Default _ action' => 'index' // DEFAULT method
? );
?>
Bytes -----------------------------------------------------------------------------------------------

Controller. class. php file

?
Class Controller
{
? Public function Run ()
? {
? $ This-> Analysis ();
? // Start to parse the URL to obtain the request controller and method
? $ Control = $ _ GET ['con '];
? $ Action = $ _ GET ['AC'];
? $ Action = ucfirst ($ action );
? // Construct the path of the controller file
? $ ControlFile = ROOT_PATH. '/Controllers/'. $ control. '. class. php ';
? If (! File_exists ($ controlFile) // if the file does not exist, an error is Prompted. otherwise
? {?
? Exit ("{$ control}. class. php controller does not exist
"." Check whether ". $ controlFile." exists.
");
? }
? Include ($ controlFile );
? $ Class = ucfirst ($ control); // capital the first letter of each word in the controller name as the class name of the controller.
? If (! Class_exists ($ class) // determines whether the class exists. If no, an error is returned.
? {
? Exit ("controller class not defined in {$ control}. class. php". $ class );
?}
? $ Instance = new $ class (); // otherwise, create an instance.
? If (! Method_exists ($ instance, $ action) // checks whether the $ action method exists in the instance $ instance. if the $ action method does not exist, an error is prompted.
? {
? Exit ("$ class does not have a method:". $ action );
?}
? $ Instance-> $ action ();
?}
?
?
?
? Protected function Analysis ()
? {
? // $ GLOBALS ['c'] ['url _ mode'];
? Global $ C; // contains the global configuration array, which is defined in the Config. ph file. The global declaration $ C calls the external
? If ($ C ['url _ mode'] = 1)
? // If the URL mode is 1, GET the controller in GET, that is, the url address is [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 ['AC'])? Trim ($ _ GET ['AC']): '';
?}
? Else if ($ C ['url _ mode'] = 2) // if it is 2, The PATH_INFO MODE is used, that is, the URL address is like this? ? [Url = http: // localhost/index. php/] http: // localhost/index. php/[/url] controller/method/Other parameters
? {
? If (isset ($ _ SERVER ['path _ info'])
? {
? // $ _ SERVER ['path _ info'] the PATH after the file name in the URL address. it is hard to understand. let's take a look at the example.
? // For example, if your current URL is [url = http://www.php100.com/index.php#http://www.php100.com/index.php#/url], your $ _ SERVER ['path _ info'] is blank.
? // However, if the URL is [url = response
? // The current $ _ SERVER ['path _ info'] value 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 );
?}
?}
? // Check whether the controller value is null. if it is null, use the default value.
? $ _ GET ['con '] =! Empty ($ control )? $ Control: $ C ['default'];
? // Same as above
? $ _ GET ['AC'] =! Empty ($ action )? $ Action: $ C ['default _ action'];
?}
}
?>
Bytes --------------------------------------------------------------------------------------------------

Welcome. class. php file

?
? Class Welcome
? {
? ? Function Index ()
? ? {
? ? Echo 'Welcome to this CMS system ';
? ?}
? ? Function Run ()
? ? {
? ? Echo 'hello ';
? ?}
? ?
? ? Function Show ()
? ? {
? ? Echo 'method name show ';
? ?}
? }

?>

?

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.