Blogger Information
Blog 48
fans 0
comment 0
visits 34162
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
composer的自动加载(0807)
丶久而旧之丶
Original
647 people have browsed it

composer自动加载

1.传统加载

  1. <?php
  2. // 加载文件
  3. require 'app/controller/User.php';
  4. // 注册类别名(没有注册的话那么就需要写命名空间或者调用时使用命名空间调用)
  5. use app\controller\User;
  6. // 调用类成员
  7. echo User::get();

2.自动加载器

  1. <?php
  2. // 使用内置函数加载
  3. spl_autoload_register(function ($class) {
  4. // 把命名空间分隔符换成目录分隔符
  5. $path = str_replace('\\', DIRECTORY_SEPARATOR, $class);
  6. // 获取文件路径
  7. $file = __DIR__ . '/' . $path . '.php';
  8. require $file;
  9. });
  10. // 注册类别名
  11. use app\controller\User;
  12. // 调用类成员
  13. echo User::get();

3.composer加载

  1. <?php
  2. // 先创建一个composer.json文件并生成vendor
  3. require __DIR__ . '/vendor/autoload.php';
  4. // 注册类别名
  5. use app\controller\User;
  6. use php\Order;
  7. use php\Show;
  8. // 调用类成员
  9. echo User::get() . '<hr>' . Order::get() . '<hr>' . Show::get();

类映射加载

psr-4方式加载

使用组件

  1. <?php
  2. // 加载组件
  3. require_once __DIR__ . '/vendor/autoload.php';
  4. // 注册类别名
  5. use Gregwar\Captcha\CaptchaBuilder;
  6. // 创建类实例
  7. $captcha = new CaptchaBuilder;
  8. $captcha->build();
  9. // 获取验证码
  10. $a = $captcha->getPhrase();
  11. echo '验证码是:' . $a;
  12. ?>
  13. <!DOCTYPE html>
  14. <html lang="en">
  15. <head>
  16. <meta charset="UTF-8">
  17. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <title>验证码演示</title>
  19. </head>
  20. <body>
  21. <h3>用户登录</h3>
  22. <div>
  23. <label for="email">邮箱:</label>
  24. <input type="email" id="email">
  25. </div>
  26. <div>
  27. <label for="password">密码:</label>
  28. <input type="password" id="password">
  29. </div>
  30. <div>
  31. <label for="yan">验证:</label>
  32. <input type="txt" id="yan">
  33. <img src="<?php echo $captcha->inline(); ?>" width='80px' onclick="location.reload()">
  34. </div>
  35. <div>
  36. <button>登录</button>
  37. </div>
  38. </body>
  39. </html>

写一个小框架

安装相应组件

  • 模型类:composer require catfan/medoo
  • 视图类:composer require league/plates

1.创建自己框架的核心代码分别继承第三方的组件

  1. <?php
  2. namespace core;
  3. use Medoo\Medoo;
  4. // 模型类(继承medoo)
  5. class Model extends Medoo
  6. {
  7. public function __construct()
  8. {
  9. $options = [
  10. 'database_type' => 'mysql',
  11. 'database_name' => 'User',
  12. 'server' => 'localhost',
  13. 'username' => 'root',
  14. 'password' => 'root'
  15. ];
  16. parent::__construct($options);
  17. }
  18. }
  1. <?php
  2. namespace core;
  3. use League\Plates\Engine;
  4. // 视图类(继承plates)
  5. class View extends Engine
  6. {
  7. public $templates;
  8. public function __construct($path)
  9. {
  10. $this->templates = parent::__construct($path);
  11. }
  12. }

2.创建自己的应用,创建属于这个应用的model,view,controller

  1. <?php
  2. namespace Models;
  3. use core\Model;
  4. // 自定义模型
  5. class AppleModel extends Model
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. }
  1. // 自定义视图
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>用户信息</title>
  8. <style>
  9. table {
  10. border: 1px solid black;
  11. border-collapse: collapse;
  12. width: 40%;
  13. margin: auto;
  14. text-align: center;
  15. }
  16. caption {
  17. font-size: 1.5rem;
  18. margin-top: 10px;
  19. margin-bottom: 20px;
  20. }
  21. th,
  22. td {
  23. border: 1px solid black;
  24. }
  25. tr:first-of-type {
  26. background-color: lightblue;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <table>
  32. <caption>用户信息</caption>
  33. <tr>
  34. <th>ID</th>
  35. <th>用户名</th>
  36. <th>性别</th>
  37. </tr>
  38. <?php foreach ($users as $user) : ?>
  39. <tr>
  40. <td><?= $this->e($user['id']) ?></td>
  41. <td><?= $this->e($user['username']) ?></td>
  42. <td><?= $this->e($user['sex']) ?></td>
  43. </tr>
  44. <?php endforeach ?>
  45. </table>
  46. </body>
  47. </html>
  1. <?php
  2. // 自定义控制器
  3. namespace Controller;
  4. class UserController
  5. {
  6. public $model;
  7. public $view;
  8. public function __construct($model, $view)
  9. {
  10. $this->model = $model;
  11. $this->view = $view;
  12. }
  13. // 用于测试
  14. public function get()
  15. {
  16. return 'PHP中文网';
  17. }
  18. public function select()
  19. {
  20. // 获取数据
  21. $users = $this->model->select('apple', ['id', 'username', 'sex'], ['id[>=]' => 10, 'LIMIT' => 10]);
  22. // 渲染数据(数据到模板引擎中)
  23. return $this->view->render('User/list', ['users' => $users]);
  24. }
  25. }

3.更新composer.json文件中的类映射关系

  1. {
  2. "require": {
  3. "catfan/medoo": "^1.7",
  4. "league/plates": "^3.3"
  5. },
  6. "autoload":{
  7. "psr-4":{
  8. "core\\":"core",
  9. "Models\\":"app/Models",
  10. "Views\\":"app/Views",
  11. "Controller\\":"app/Controllers"
  12. }
  13. }
  14. }

4.创建入口文件,使用composer加载器

  1. <?php
  2. require __DIR__ . "/vendor/autoload.php";
  3. use Models\AppleModel;
  4. use core\View;
  5. use Controller\UserController;
  6. // 测试模型
  7. $model = new AppleModel();
  8. // var_dump($model);
  9. // echo '<hr>';
  10. // 测试视图
  11. $view = new View('app/Views');
  12. // var_dump($view);
  13. // echo '<hr>';
  14. // 测试控制器
  15. $controller = new UserController($model, $view);
  16. // var_dump($controller);
  17. echo $controller->select();

总结

1.了解了composer的自动加载
2.框架的书写大概了解了,对于各组件的使用还需多看手册了解其功能

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