Blogger Information
Blog 145
fans 7
comment 7
visits 164444
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP实战:利用composer写一个MVC小框架
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
Original
1287 people have browsed it

一.MVC框架核心组成:

1.MVC框架大体可分为model、view、controller,这三块主要都是类、对象的应用和扩展
2.关于composer组件是对PHP代码模块化的一种形式,代码封装,再利用;
3.MVC框架整体构架没有变化,只是在MVC的架构中添加了composer组件管理器,来方便添加组件功能;
4.对于框架可以整体理解为三部分:MVC框架部分{app和core}和组件部分{vendor和composer.json、composer.lock}以及入口文件;
5.MVC框架重点理解:命名空间和自动加载之间的关系;往往出问题都在自动加载这块,自动加载出问题:往往跟命名空间、类名称和文件路径不一致导致;注意:\/的使用场景

二.实战案例(利用数据库catfan/medoo和视图league/plates写一个个MVC小框架)

1.文件目录

2.json文件中自动加载配置项

  1. {
  2. "name": "ldy/frame",
  3. "description": "MVC小框架",
  4. "require": {
  5. "catfan/medoo": "^1.7",
  6. "league/plates": "^3.4"
  7. },
  8. //自动加载配置项
  9. "autoload": {
  10. "psr-4": {
  11. "app\\Controllers\\": "app/Controllers",
  12. "app\\Models\\": "app/Models",
  13. "app\\View\\": "app/View",
  14. "core\\": "core"
  15. }
  16. }
  17. }

3.控制类代码

  1. <?php
  2. namespace app\Controllers;
  3. use app\Models\StaffsModel;
  4. use core\View;
  5. class StaffsController
  6. {
  7. private $model=null;
  8. private $view=null;
  9. public function __construct(StaffsModel $model,View $view){
  10. $this->model=$model;
  11. $this->view=$view;
  12. }
  13. public function index(){
  14. $data=$this->model->select("v_staffs",["id","name","age","gender","salary","email","postion","area"],["LIMIT"=>10]);
  15. echo $this->view->render("index",["rows"=>$data]);
  16. }
  17. }

4.入口文件代码

  1. <?php
  2. use app\Controllers\StaffsController;
  3. use app\Models\StaffsModel;
  4. use core\View;
  5. include __DIR__."/vendor/autoload.php";
  6. $model=new StaffsModel();
  7. $view=new View("app/View/staffs");
  8. (new StaffsController($model,$view))->index();

5.运行结果图

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post