PHP Simple MVC Framework Implementation method _php instance

Source: Internet
Author: User

Using MVC in PHP has become increasingly popular, especially in some open source frameworks.

1. Overview

The MVC full name is Model view Controller, an abbreviation for Models-View-Controller (Controller), an example of software design, which organizes code using a business logic, data, and interface to display a separate method. Clustering business logic into a single component, without having to rewrite the business logic while improving and customizing 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 calls function functions C ($name, $method) {require_once (' libs/controller/'. $name. ' Control
    Ler.class.php ');
    $testController = new TestController ();
    $testController->show (); Eval (' $obj = new '. $name. ' Controller (); $obj-> '. $method. '
  ();');
    The//model calls functions function M ($name) {require_once (' libs/model/'. $name. ' Model.class.php ');
    Eval (' $obj = new '. $name. ' Model (); ');
  return $obj;
    //view calls function Functions 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/* First step browser-> call controller, give it instructions second step controller-> select an appropriate model according to instructions the third step model-> the corresponding data according to the controller instruction fourth step controller, ;
Select the appropriate view by instruction step Fifth view-> the data taken by the third step according to the user want to show out * * * require_once (' view/testview.class.php ');
Require_once (' model/testmodel.class.php '); Require_once (' CONTROLLER/TESTCONTROLLER.CLASS.PHP ');
$testController = new TestController (); $testController->show ();?> <?php//testcontroller.class.php/* The role of the controller is to call the model and call the view to pass the data generated by the model to the view and have the relevant view to display *
      /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 function of the?> <?php//testmodel.class.php/* model is to get the data and process it, returning the data/class testmodel{function getting () {return "H"
    Ello World "; The function of the?> <?php//testview.class.php/* View is to organize and beautify the data obtained, and ultimately to output the */class testview{function display to the user terminal ($da
    TA) {echo $data; }}?>

Run Result:


MVC in PHP

MVC[1] is a software architecture in software engineering. There are some differences in MVC from a PHP perspective.

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

View (views), graphical interface logic. Responsible for output in PHP, dealing with how to call the template, the required resource files.

Controller (Controller), responsible for forwarding requests, processing requests. In PHP, the calling view and the data used are determined according to the request.

Why use MVC

The main role of MVC is to tier and categorize code.

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

In this process also found some other advantages, the site's directory structure more clearly, the site 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.