webman:自动加载(v1.5.7)

发布时间 2023-09-22 11:19:38作者: 刘宏缔的架构森林

一,官方文档地址:

https://www.workerman.net/doc/webman/others/autoload.html

二,实际操作

1,设置目录:

在项目根目录下新建目录 extend:

修改composer.json

增加以下代码:

"psr-0" : {
    "": "extend/"
}

如图:

执行dumpautoload

liuhongdi@lhdpc:/data/webman/imageadmin$ composer dumpautoload
Generating autoload files
Generated autoload files

2,演示代码:

类文件

extend/captcha/captcha.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
namespace captcha;
use Webman\Captcha\CaptchaBuilder;
use Webman\Captcha\PhraseBuilder;
 
class captcha {
      static public function getCaptcha($codeLength,$width,$height) {
// 验证码长度
          //$length = 4;
          // 包含哪些字符
          $chars = '0123456789abcefghijklmnopqrstuvwxyz';
          $builder = new PhraseBuilder($codeLength, $chars);
          $captcha = new CaptchaBuilder(null, $builder);
 
          // 生成验证码
          $captcha->build($width, $height, $font = null);
          // 将验证码的值存储到session中
          //$request->session()->set('captcha', strtolower($captcha->getPhrase()));
          /*
          // base64 image
          $image = $captcha->inline();
          //json
          return json(['code' => 0, 'image'=>$image,'uniqid'=>'123']);
          */
          // 获得验证码图片二进制数据
          $img_content = $captcha->get();
          //return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
          return $img_content;
      }
 
      static public function checkCaptcha() {
 
      }
}

controller中调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
namespace app\controller;
 
use support\Request;
 
use captcha\captcha;
 
class ImageController
{
 
    /**
     * 输出验证码图像
     */
    public function captcha(Request $request){
 
        $img_content = captcha::getCaptcha(4,100,40);
        return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
    }

三,测试效果:

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/09/20/webman-zi-dong-jia-zai-v1-5-7/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

四,查看webman框架的版本:

liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name     : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...