当前 Thinkphp 版本 v5.1.39 LTS
由于tp5.1的图像验证码功能是基于session的,且不支持拓展成其他存贮方式。
在前后端完全分离的情况下,使用Jwt鉴权,验证码功能直接废了。所以我改装了一下 tp5.1 的验证码功能模块,通过 cache 的方式实现前后端分离的情况下,对验证码进行验证。
另外使用 Composer 方式创建一个库前后端分离的图片验证码库
1、建立一个仓库
首先我们到github上建立一个仓库,名字就叫 think-jwt-captcha 好了。
将仓库克隆到本地
git clone git@github.com:18y/think-jwt-captcha.git
2、初始化项目
然后初始化项目,composer init 生成 composer.json 文件, 详细步骤如下
PS D:\phpstudy_pro\WWW\test\think-jwt-captcha> composer -V Composer version 2.0.2 2020-10-25 23:03:59 PS D:\phpstudy_pro\WWW\test\think-jwt-captcha> composer self-update Upgrading to version 2.0.8 (stable channel). Use composer self-update --rollback to return to version 2.0.2 PS D:\phpstudy_pro\WWW\test\think-jwt-captcha> composer init Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (<vendor>/<name>) [administrator/think-jwt-captcha]: 18y/think-jwt-captcha Description []: thinkphp前后端分离图像验证码 Author [xiadmin <cmphper@163.com>, n to skip]: xiadmin <cmphper@163.com> Minimum Stability []: dev Package Type (e.g. library, project, metapackage, composer-plugin) []: library License []: MIT Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? yes Search for a package: php Enter the version constraint to require (or leave blank to use the latest version): >=5.6.0 Search for a package: topthink/think-captcha Enter the version constraint to require (or leave blank to use the latest version): ^2.0 Search for a package: Would you like to define your dev dependencies (require-dev) interactively [yes]? Search for a package: { "name": "18y/think-jwt-captcha", "description": "thinkphp", "type": "library", "require": { "php": ">=5.6.0", "topthink/think-captcha": "^2.0" }, "license": "MIT", "authors": [ { "name": "xiadmin", "email": "cmphper@163.com" } ], "minimum-stability": "dev" } Do you confirm generation [yes]? yes Would you like the vendor directory added to your .gitignore [yes]? yes Would you like to install dependencies now [yes]? no
3、完善项目代码
完成1、2步,已经算成功一大半啦。这一步我们先不安装项目,先把代码写完
新增 src 目录并完善分离的验证码代码,我这里只需要一个 JwtCaptcha.php 文件就算写完啦
添加完成后当前项目目录结构如下
├─think-jwt-captcha
│ ├─src
│ │ ├─JwtCaptcha.php
│ └─composer.json
修改 composer.json 文件将 JwtCaptcha 代码添加到自动加载
添加以下代码
"autoload": { "psr-4": { "JwtCaptcha\\": "src/" } }
意思是将当前src目录的文件全部加载。最终composer.json 文件如下
{ "name": "18y/think-jwt-captcha", "description": "thinkphp", "type": "library", "require": { "php": ">=5.6.0", "topthink/think-captcha": "^2.0" }, "license": "MIT", "authors": [ { "name": "xiadmin", "email": "cmphper@163.com" } ], "minimum-stability": "dev", "autoload": { "psr-4": { "JwtCaptcha\\": "src/" } } }
执行 composer install
,这时会生成vendor
目录,及其他文件。如果修改了composer.json 的代码务必执行一下
composer update
才能正确更新项目代码
4、推送代码到 GitHub 仓库
git add . git commit -m 'init' git tag v1.0.0 // 记住打一个版本号 git push origin master git push v1.0.0
然后将 GitHub 上的包提交到 Packagist
- 首先要在 Packagist 上注册账号并登录(可以用 GitHub 直接登录)
- 点击顶部导航条中的 Summit 按钮
- 在输入框中输入 GitHub 上的刚才包地址,如:https://github.com/18y/think-jwt-captcha
- 然后点击 Check 按钮 Packagist 会去检测此仓库地址的代码是否符合 Composer 的 Package 包的要求
检测正常的话,会出现 Submit 按钮,再点击一下 Submit 按钮,我们的包就提交到 Packagist 上了
然后在 thinkphp5 项目中尝试使用我们的插件
5、安装刚刚上传的插件
composer require 18y/think-jwt-captcha