This article mainly introduces php's simple MVC framework, and analyzes in detail the implementation skills and precautions of php's MVC framework, which has some reference value, for more information about php implementation, see the following example. Share it with you for your reference. The details are as follows:
Knowledge you need to know before you start
1. basic php knowledge
2. single entrance, don't know can look here (http://www.bitsCN.com/article/72621.htm)
With the above two points, we can start. haha!
First, let's talk about the execution process of the program.
First, there is an entry file, then initialize some programs, and then call different classes and methods according to the request.
First, let's get an entry file Index. php to check the code.
<?phprequire "Init.php";$control = new Controller();$control->Run();?>
The code is nothing special. first introduce the Init. php file and then instantiate a class.
Then call the Run () method of this class. here we call this class controller.
Since the Init. php file is introduced, let's continue to look at the source code of the Init. php file.
<? Phpheader ("Content-type: text/html; charset = utf-8 ");! Defined ('root _ path') & define ('root _ path', str_replace ('\', '/', dirname (_ FILE __))); require ROOT_PATH. '/Core/Config. php'; // introduce the configuration file require ROOT_PATH. '/Core/Controller. class. php '; // introduce the controller class file require ROOT_PATH. '/Core/View. class. php '; // View class file require ROOT_PATH. '/Core/Model. class. php'; // model file?>
Analyze the code. if you understand the code, skip this step and continue to look at it.
First set the character set, and then determine if the constant "ROOT_PATH" is not defined, then define it.
Then, some files are introduced. The first is the configuration file, controller class file, view class file, and model class file.
Similarly, since a file is introduced, open the file and check the code. First, check the Config. php file.
<? Php $ C = array ('URL _ mode' => 1, // url mode, 1 Normal MODE, 2 PATH_INFO MODE 'default _ control' => 'Welcome ', // The DEFAULT controller called 'default _ action' => 'index', // the DEFAULT method);?>
Nothing special is an array, which has three values. this is the first option for the time being. it will be added later.
Well, let's continue to look at Controll. class. php.
<? Phpclass Controller {public function Run () {$ this-> Analysis (); // Start to parse the URL to obtain the request's Controller and method $ control =$ _ GET ['c']; $ action = $ _ GET ['A']; // 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, an error is Prompted. otherwise, {exit ('Controller does not exist' is introduced '. $ controlFile);} include ($ controlFile); $ class = ucwords ($ control); // use 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 error exists, {exit ('define controller class '. $ 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 {exit ('Nonexistent method' is displayed '. $ action) ;}$ instance-> $ action () ;} protected function Analysis () {global $ C; // contains the global configuration array, which is in Config. if ($ C ['url _ mode'] = 1) defined in the ph file // if the url mode is 1, obtain the controller in GET, that is to say, the url address is like this http://localhost/index.php ? C = controller & a = method {$ control =! Empty ($ _ GET ['c'])? Trim ($ _ GET ['c']): ''; $ action =! Empty ($ _ GET ['A'])? Trim ($ _ GET ['A']): '';} else if ($ C ['url _ mode'] = 2) // if it is 2, The PATH_INFO mode is used, that is, the url address is like this. http://localhost/index.php /Controller/method/other parameters {if (isset ($ _ SERVER ['path _ info']) {// $ _ SERVER ['path _ info'] indicates the PATH information after the file name in the URL address. it is hard to understand. let's take a look at the example. // for example, your current URL is http://localhost/index.php Your $ _ SERVER ['path _ info'] is null //, but if the URL is http://localhost/index.php /Abc/123 // the current $ _ SERVER ['path _ info'] value will be index. php file name content/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 $ _ GET ['c'] = $ control =! Empty ($ control )? $ Control: $ C ['default _ control']; // same as above $ _ GET ['A'] = $ action =! Empty ($ action )? $ Action: $ C ['default _ action'] ;}}?>
The annotations are clearly written. I will not talk about them here. to do this, you can create a Controller directory and then create the welcome. class. php file in the directory.
Write the following content
<?phpclass Welcome{ public function index() { echo 'Hello'; }}?>
Allow the program and you will see Hello
Then write a method
<? Phpclass Welcome {public function index () {echo 'hello';} public function show () {echo 'method name Show' ;}}?>
I hope this article will help you with php programming.