Simple single-entry MVC implementation

Source: Internet
Author: User

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
    1. Require("Config. php");
    2. Require("Function. php");
    3. Require("Dispatcher. php");

Let's take a look at config. php, which defines some constants.

PHP code
  1. // Server info
  2. Define ("Host","Localhost");
  3. Define ("User","Root");
  4. Define ("Password","123456");
  5. Define ("Database","Article");
  6. // Path Info
  7. Define ("Include_path", Dirname (_ File __));
  8. // Controller
  9. Define ("Controller_path", Include_path."/Controller");

Function. php defines some basic functions.

PHP code
  1. // Format exception
  2. FunctionFormat_exception (exception$ E){
  3. Print <EOT
  4. <Div style ="Margin: 200px auto; width: 400px; Border: 1px solid # d5924d; Background: # efebe7; font-size: 14px; text-align: center; padding: 20px">
  5. {$ E-> Getmessage ()} </div>
  6. <! -<Meta HTTP-EQUIV = Refresh content = '1; url = javascript: history. Go (-1) '>->
  7. EOT;
  8. Die();
  9. }
  10. // Get controller and init Controller
  11. FunctionGet_controller ($ Controller_name){
  12. If(!File_exists(Controller_path."/{$ Controller_name}. php")){
  13. Throw NewException ("There is no such a controller named <font color = Red> $ controller_name </font>");
  14. }
  15. Else{
  16. Require(Controller_path."/{$ Controller_name}. php");
  17. }
  18. If(!Class_exists($ Controller_name)){
  19. Throw NewException ("There is no class named {$ controller_name }");
  20. }
  21. }

Finally, let's take a look at dispatch. php, which mainly introduces the target file and instantiate it.

PHP code
  1. $ Controller=$ _ Get["Controller"]."Controller";
  2. $ Action= Isset ($ _ Get["Action"])?$ _ Get["Action"]."Action":"Indexaction";
  3. Try {
  4. Get_controller ($ Controller);
  5. $ Controller=New $ Controller();
  6. $ Controller->$ Action();
  7. }
  8. Catch (exception$ E){
  9. Format_exception ($ E);
  10. }

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
    1. Order deny, allow
    2. Deny from all
Related Article

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.