1. Add the code in the controller
public function actionCaptcha(){ $c = Yii::createObject('yii\captcha\CaptchaAction', ['__captcha', $this]); $c->getVerifyCode(true); return $c->run(); }
2. Call the verification code through the following method
$c = Yii::createObject('yii\captcha\CaptchaAction', ['__captcha', $this]); $code = $c->getVerifyCode();
$code is generated before Verification code
Note: $this represents the current controller, this method is only suitable for use in one controller.
3. Front-end call:
Write a
PHP Chinese website has a large number of free Yii introductory tutorials, everyone is welcome to learn!
The above is the detailed content of How to generate verification code in yii2. For more information, please follow other related articles on the PHP Chinese website!