The followingthinkphp frameworkThe tutorial column will explain to you the issue of email verification in Thinkphp5.1. I hope it will be helpful to friends in need!
Specific question:
For example, I want to verify whether this email is legitimate. I want to use TP's own verification rules. How should I verify it? I see that the manual requires defining a User class. We define a \app\index\validate\User validator class for User verification. Is it so troublesome for the TP framework to verify email usernames? Where should this validator class be written? Is it in the same directory as the controller?
<?php namespace app\index\controller; use think\Controller; use think\facade\Request; use think\response; use think\View; use think\Validate; class Register extends Controller { public function regcheck(){ $data=input('email'); } } ?>
Solution:
If you want a single verification, you can call it statically
// 验证是否有效邮箱地址 use think\facade\Validate; Validate::isEmail('thinkphp@qq.com'); // true
If there are many things to verify, it is recommended It is recommended to use a validator
The validator class can customize the directory, and it is recommended to place it in the \app\index\validate directory.
Validator class
namespace app\index\validate; use think\Validate; class User extends Validate { protected $rule = [ 'name' => 'require|max:25', 'email' => 'email', ]; protected $message = [ 'name.require' => '名称必须', 'name.max' => '名称最多不能超过25个字符', 'email' => '邮箱格式错误', ]; }
Use in the controller:
namespace app\index\controller; use think\Controller; class Index extends Controller { public function index() { $data = [ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com', ]; $validate = new \app\index\validate\User; if (!$validate->check($data)) { dump($validate->getError()); } } }
The above is the detailed content of Let's talk about the issue of email verification in Thinkphp5.1. For more information, please follow other related articles on the PHP Chinese website!