Example Analysis of MVC Development Mechanism of ThinkPHP

  • 2021-07-16 02:07:43
  • OfStack

ThinkPHP is a widely used MVC development framework in China. This paper analyzes the MVC development mechanism of ThinkPHP in the form of examples. I believe it will give you an enlightening effect. The specific analysis is as follows:

1. Overview:

The MVC framework parses as follows:

M Model layer model: It is the database operation class (through the database operation class to operate each table)

V View Layer View: Refers to the template.

C Control layer controller: It is through the controller to achieve the control relationship between templates and models.

2. Example analysis:

1. ACTION controller:

Location D:\ www\ aoli\ Home\ Lib\ Action\ IndexAction. class. php

The code is as follows:


  public function test() // Access path: http://localhost/aoli/index.php/Index/test
  {
    $Test=D('Test');// Instantiation Model
    //$list=$Test->select();
    $list=$Test->query("select * from test" );
    $this->assign('list',$list);
    $this->assign('title',' Peng Yanjie ');
    $this->display();
  }
  public function index() //index Correspondence aoli\Tpl\default\Index Under index.html
  {
    $this->display();
  }

2. MODEL model:

Location D:\ www\ aoli\ Home\ Lib\ Model\ IndexModel.class.php

The code is as follows:


<?php
class TestModel extends Model{ // Corresponding tables in the database test
 // Here you can add classes that manipulate database tables 
}
?>

3. VIEW view:

Location D:\ www\ aoli\ Home\ Tpl\ default\ Index\ test.html

The code is as follows:


 <p style=" font-weight:bold; line-height:22px;">{$title}</p>
 <div style=" color:#00F;">
  <volist name="list" id="vo">
   <p>{$vo.title} - {$vo.con}</p>
  </volist>
 </div>

Interested friends can debug and run the example described in this article under 1 to deepen their understanding. I hope this article is helpful for everyone to learn ThinkPHP.


Related articles: