yii2.0生成验证码

可能对于很多像我这样yii的菜鸟级的同志,每次百度到的答案是一长段复杂的代码,内心是崩溃的。所以下面就尽我可能简单的实现验证码

1.现在你的控制器里写上这些代码

<?php
namespace frontendcontrollers;

use yiiwebController;

class IndexController extends Controller
{
    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yiicaptchaCaptchaAction',
                //'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                'backColor' => 0x000000,//背景颜色
                'maxLength' => 6, //最大显示个数
                'minLength' => 5,//最少显示个数
                'padding' => 5,//间距
                'height' => 40,//高度
                'width' => 130,  //宽度
                'foreColor' => 0xffffff,     //字体颜色
                'offset' => 4,        //设置字符偏移量 有效果
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }
}

2.在页面 index.php 里写入一下代码,注意:captchaAction'=>'index/captcha’是写对应的控制器

<?php
use yiicaptchaCaptcha;?>

<?php
echo Captcha::widget(['name'=>'captchaimg','captchaAction'=>'index/captcha',
    'imageOptions'=>['id'=>'captchaimg', 'title'=>'换一个', 'alt'=>'换一个', 'style'=>'cursor:pointer;margin-left:25px;'],'template'=>'{image}']);

然后打开页面:index/index 就呈现这样的效果

ok,就这样完成了。

原文地址:https://www.cnblogs.com/fangjiali/p/6824394.html