yii2.0生成验证码


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

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

<?php
namespace frontend\controllers;

use yii\web\Controller;

class IndexController extends Controller
{
    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                //'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 yii\captcha\Captcha;?>

<?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,就这样完成了。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM