PHP Simple MVC Framework Implementation Method _php example

Source: Internet
Author: User
The use of MVC in PHP has become increasingly popular, especially in some open-source frameworks.

1. Overview

The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.

2. Code structure

3. Code implementation

<?php//function.php//Controller call function C ($name, $method) {require_once (' libs/controller/'. $name. ' Controller.c    Lass.php ');    $testController = new TestController ();    $testController->show (); Eval (' $obj = new '. $name. ' Controller (); $obj. $method. '  ();');    }//model calls function M ($name) {require_once (' libs/model/'. $name. ' Model.class.php ');    Eval (' $obj = new ' $name. ' Model (); ');  return $obj;    }//View calls function V ($name) {require_once (' libs/view/'. $name. ' View.class.php ');    Eval (' $obj = new ' $name. ' View (); ');  return $obj;  }//Filter illegal value function daddslashes ($STR) {return (!GET_MAGIC_QUOTES_GPC ())? Addslashes ($STR): $str; }?><?php//test.php/* The first step browser-----Call the controller, give it a command. Second step controller, select a suitable model by command the third step model, according to the controller instructions to take the corresponding data fourth step controller The data to be taken from the third step should be displayed as the user wants in the fifth step view of the View */require_once (' view/testview.class.php '); require_once (' model/ TestModel.class.php '); require_once (' controller/testcontroller.class.php '); $testController = new TestControlLer (); $testController->show ();?    The role of the ><?php//testcontroller.class.php/* controller is to invoke the model, invoke the view, pass the data generated by the model to the view, and have the relevant view display the */class testcontroller{      Function Show () {/* $testModel = new Testmodel ();      $data = $testModel->get ();      $testView = new TestView ();      $testView->display ($data); */$testModel = M (' test ');      $data = $testModel->get ();      $testView = V (' test ');    $testView->display ($data); The role of the}?><?php//testmodel.class.php/* model is to get the data and process it, return the data */class testmodel{function get () {return "Hello World"    ";      The function of the}?><?php//testview.class.php/* view is to organize, beautify, and eventually output the obtained data to the user terminal */class testview{function display ($data) {    Echo $data; }}?>

Operation Result:


MVC in PHP

MVC[1] is a software architecture in software engineering. There are some differences in MVC from the point of view of PHP.

Model (models), the implementation of program application functions, the implementation of the logic of the program. Responsible for data management and data generation in PHP.

View, graphical interface logic. Responsible for the output in PHP, processing how to invoke the template, the required resource files.

Controller, which is responsible for forwarding the request and processing the request. The views and data used in PHP are determined on request.

Why use MVC

The main purpose of MVC is to layer and categorize code.

The main purpose of MVC is to solve the separation development and design work in web development, and make it work relatively independent.

In this process also found some other advantages, the site's directory structure is more clear, the site is easier to maintain and expand, you can achieve the reuse of modules.

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