Blogger Information
Blog 55
fans 3
comment 0
visits 54026
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
composer自动加载器和自撸框架
王佳祥
Original
892 people have browsed it

composer自动加载器和自撸框架

一、composer自动加载器



1.传统方式加载类文件

  1. <?php
  2. //1.传统方式:include / require
  3. //加载类文件
  4. require 'app/controller/User.php';
  5. //注册类别名
  6. use app\controller\User;
  7. //调用类成员
  8. echo User::hello();


2.注册自动加载函数

  1. <?php
  2. //2.注册自动加载函数
  3. //加载类文件使用一个内置的函数来完成
  4. spl_autoload_register(function($class){
  5. //获取类的路径名称
  6. $path = str_replace('\\',DIRECTORY_SEPARATOR,$class);
  7. //生成一个完整的类文件名
  8. $classFile = __DIR__ . '/' . $path . '.php';
  9. //加载类文件
  10. require $classFile;
  11. });
  12. use app\controller\User;
  13. echo User::hello();


3.使用composer的自动加载

  1. <?php
  2. //3.使用composer中的自动加载
  3. //调用的是composer中的自动加载功能
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. use app\controller\User;
  6. echo User::hello();
  • 在composer.json文件中注册自动加载 命名空间\\ :路径
    1. {
    2. "autoload": {
    3. "psr-4": {
    4. "app\\controller\\": "app/controller"
    5. }
    6. },
    7. "require": {
    8. "gregwar/captcha": "^1.1"
    9. }
    10. }


4.在项目中使用第三方依赖库/组件

  1. <?php
  2. //如何在项目中使用第三方依赖库/组件
  3. //composer自动加载器
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. //注册类(验证码)
  6. use Gregwar\Captcha\CaptchaBuilder;
  7. $captcha = new CaptchaBuilder;
  8. $captcha
  9. ->build()
  10. ->save('out.jpg');
  11. //获取验证码
  12. $_SESSION['phrase'] = $captcha->getPhrase();
  13. echo $_SESSION['phrase'];
  14. ?>
  15. <!DOCTYPE html>
  16. <html lang="en">
  17. <head>
  18. <meta charset="UTF-8">
  19. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  20. <title>Document</title>
  21. </head>
  22. <body>
  23. <table>
  24. <caption>用户登录</caption>
  25. <tr>
  26. <td>邮箱:</td>
  27. <td><input type="email"></td>
  28. </tr>
  29. <tr>
  30. <td>密码:</td>
  31. <td><input type="password"></td>
  32. </tr>
  33. <tr>
  34. <td>验证码:</td>
  35. <td><img src="<?php echo $captcha->inline();?>" onclick="location.reload()" alt=""></td>
  36. </tr>
  37. <tr>
  38. <td colspan="2"><button>登录</button></td>
  39. </tr>
  40. </table>
  41. </body>
  42. </html>


二、自撸框架

1.采用mvc架构

  • 模型,视图,控制器

2.下载第三方模型、视图包

  • 模型用medoo包,用终端下载:输入composer require catfan/medoo命令

  • 视图用plates包,输入composer require league/plates命令

3.创建自己的核心代码,分别继承第三方包

  • Model.php继承medoo包
  1. <?php
  2. //模型类
  3. namespace core;
  4. use Medoo\Medoo;
  5. //继承自第三方包:catfan/medoo
  6. class Model extends Medoo
  7. {
  8. //构造方法:连接数据库
  9. public function __construct()
  10. {
  11. $options = [
  12. 'database_type'=>'mysql',
  13. 'database_name'=>'tp5',
  14. 'server'=>'localhost',
  15. 'username'=>'root',
  16. 'password'=>'wang1111'
  17. ];
  18. parent::__construct($options);
  19. }
  20. }
  • View.php继承plates包
  1. <?php
  2. //视图类
  3. namespace core;
  4. use League\Plates\Engine;
  5. class View extends Engine
  6. {
  7. public $templates;
  8. public function __construct($path)
  9. {
  10. $this->templates = parent::__construct($path);
  11. }
  12. }

4.按MVC框架创建属于自己的应用

  • 创建一个目录,目录下再按mvc框架创建三个子目录,model、view、controller


  • 模型:UsersModel.php
  1. <?php
  2. namespace models;
  3. use core\Model;
  4. //用户自定义模型通常与一张数据表对应
  5. class UsersModel extends Model
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. }
  • 控制器:UsersController.php
  1. <?php
  2. namespace controllers;
  3. class UsersController
  4. {
  5. //将依赖的模型对象和视图对象,使用依赖注入到构造方法中,使它在控制器中共享
  6. public $model = null;
  7. public $view = null;
  8. //构造方法
  9. public function __construct($model,$view)
  10. {
  11. $this->model = $model;
  12. $this->view = $view;
  13. }
  14. public function index()
  15. {
  16. return __METHOD__;
  17. }
  18. public function select()
  19. {
  20. $users = $this->model->select('user',['id','name','username'],['LIMIT'=>5]);
  21. //将数据渲染到模板上(模板赋值同步完成)
  22. return $this->view->render('users/list',['users'=>$users]);
  23. }
  24. }
  • 视图:list.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>用户信息</title>
  7. </head>
  8. <body>
  9. <table border="1" cellpadding="10px" cellspacing="0">
  10. <caption>用户管理</caption>
  11. <tr>
  12. <th>id</th>
  13. <th>姓名</th>
  14. <th>账号</th>
  15. </tr>
  16. <?php foreach($users as $user): ?>
  17. <tr>
  18. <td><?=$this->e($user['id'])?></td>
  19. <td><?=$this->e($user['name'])?></td>
  20. <td><?=$this->e($user['username'])?></td>
  21. </tr>
  22. <?php endforeach; ?>
  23. </table>
  24. </body>
  25. </html>

5.用composer.json实现自动加载

  1. {
  2. "require": {
  3. "catfan/medoo": "^1.7",
  4. "league/plates": "^3.3"
  5. },
  6. "autoload": {
  7. "psr-4": {
  8. "models\\": "app/models",
  9. "core\\": "core",
  10. "views\\": "app/views",
  11. "controllers\\": "app/controllers"
  12. }
  13. }
  14. }
  • composer dumpautoload 更新类的映射关系

6. 创建入口文件index.php

  1. <?php
  2. use models\UsersModel;
  3. use core\View;
  4. use controllers\UsersController;
  5. //composer加载器
  6. require_once __DIR__ . '/vendor/autoload.php';
  7. //测试模型
  8. $model = new UsersModel();
  9. //var_dump($model);
  10. //测试视图
  11. $view = new View('app/views');
  12. //var_dump($view);
  13. //测试控制器
  14. $controller = new UsersController($model,$view);
  15. //var_dump($controller);
  16. print_r($controller->select());
  • 测试模型:


  • 测试视图:


  • 测试控制器




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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!