Watch Star Fork

东城影 / GACaptchaPHPMIT

GACaptcha is a captcha component for Yii2.
克隆/下载 1.49 KB



composer require ga/captcha dev-master


1.Set up following code in actions method of SiteController.(Default) You can also change the "url" route what you want, but you should change the captchaAction property for CaptchaWidget in the next step.

Class SiteController extend yii\web\Controller
public function actions()
        return [
                'class' => 'ga\captcha\CaptchaAction',

2.Create Widget in the view file, and it's support the active mode.

use app\models\CaptchaWidget;
use yii\helpers\Html;
<?= Html::beginForm('site/test', 'post')?>
<?= CaptchaWidget::widget([
    'name' => 'captcha',
    'template' => '<label for="captcha">Captcha</label>&emsp;&emsp;{input}{image}',
    'options' => ['id' => 'captcha'],
<?= Html::submitButton('Submit')?>
<?= Html::endForm()?>

NOTE: If you change the url route, and you need to set the captchaAction property.

<?= CaptchaWidget::widget([
    'captchaAction'=> 'controller/action'

3.Config your method need to validate the verification code.

public function actionTest(){

    $res = Yii::$app->request->post('captcha');
    $ca = new CaptchaValidator();
        echo "success";
        echo "fail";


Thanks for the author of HansKendrickV-Regular.ttf to provide the nice free font.

项目点评 ( 0 )