MVC is not much said. It is everywhere on the Internet. All kinds of frameworks are basically based on MVC, but the cost of learning a template is quite high, and efficiency is also a problem, so I implemented it myself.
Let's take a look at index. php. There are three sentences in total. Of course, this is the most basic. Other functions can be expanded by yourself.
PHP Code
- Require("Config. php");
- Require("Function. php");
- Require("Dispatcher. php");
Let's take a look at config. php, which defines some constants.
PHP code
-
- // Server info
-
- Define ("Host","Localhost");
- Define ("User","Root");
-
- Define ("Password","123456");
-
- Define ("Database","Article");
-
-
- // Path Info
-
- Define ("Include_path", Dirname (_ File __));
-
-
- // Controller
-
- Define ("Controller_path", Include_path."/Controller");
Function. php defines some basic functions.
PHP code
-
- // Format exception
-
- FunctionFormat_exception (exception$ E){
-
- Print <EOT
-
- <Div style ="Margin: 200px auto; width: 400px; Border: 1px solid # d5924d; Background: # efebe7; font-size: 14px; text-align: center; padding: 20px">
-
- {$ E-> Getmessage ()} </div>
-
- <! -<Meta HTTP-EQUIV = Refresh content = '1; url = javascript: history. Go (-1) '>->
- EOT;
-
- Die();
-
- }
-
-
- // Get controller and init Controller
-
- FunctionGet_controller ($ Controller_name){
-
- If(!File_exists(Controller_path."/{$ Controller_name}. php")){
- Throw NewException ("There is no such a controller named <font color = Red> $ controller_name </font>");
-
- }
-
- Else{
-
- Require(Controller_path."/{$ Controller_name}. php");
-
- }
- If(!Class_exists($ Controller_name)){
-
- Throw NewException ("There is no class named {$ controller_name }");
-
- }
-
- }
Finally, let's take a look at dispatch. php, which mainly introduces the target file and instantiate it.
PHP code
-
- $ Controller=$ _ Get["Controller"]."Controller";
- $ Action= Isset ($ _ Get["Action"])?$ _ Get["Action"]."Action":"Indexaction";
-
- Try {
-
- Get_controller ($ Controller);
-
-
- $ Controller=New $ Controller();
-
- $ Controller->$ Action();
-
-
- }
-
-
- Catch (exception$ E){
-
- Format_exception ($ E);
-
- }
File Format
The Controller naming rule is: controller name + "controller", for example: "articlecontroller". The naming rule for a method is: method name + "action", for example: "readaction"
No model writing function. You only need to introduce this function in the Controller. For example:
Require (include_path. "/model/articlemodel. php ");
The URL is like this when calling: "http://www.xxxx.com/index.php? Controller = article & Action = read ". If you want to add other parameters, you can add them to the end and upload them to the corresponding action.
Finally, to prevent browsing directly from the Controller folder, you can add. htaccess. The content is as follows:
PHP code
- Order deny, allow
- Deny from all