zoukankan      html  css  js  c++  java
  • [moka同学笔记]Yii2.0验证码

    1.Model中Code.php

    <?php
    /**
     * Created by PhpStorm.
     * User: moka同学
     * Date: 2016/07/25
     * Time: 10:48
     */
    namespace appmodels;
    
    use yiiaseModel;
    
    class Code extends Model{
        public $verifyCode;
    
        public function rules()
        {
            return [
                ['verifyCode','captcha','captchaAction'=>'code/captcha','message'=>'验证码错误!']
            ];
        }
    }
    ?>

    2.控制器中CodeController.php

    <?php
    /**
     * Created by PhpStorm.
     * User: moka同学
     * Date: 2016/07/25
     * Time: 10:46
     */
    namespace appcontrollers;
    
    use appmodelsCode;
    use yiiwebController;
    
    class CodeController extends Controller{
        public function actions()
        {
            return [
                //验证码
                'captcha'=>[
                    //验证码类
                    'class'=>'yiicaptchaCaptchaAction',
                    'maxLength'=>6, //生成验证码的长度最大为4
                    'minLength'=>4, //生成最小个数4
                    'width' =>80,   //宽度
                    'height'=>40
                ]
            ];
        }
    
        public function actionIndex(){
            $code = new Code();
            if(Yii::$app->request->isPost){
                //验证码验证
                if($code->validate()){
                    echo "验证通过";
                }else{
                    var_dump($code->getErrors());
                }
            }
    
            return $this->render('index',['model'=>$code]);
    
        }
    }
    ?>

    3.视图index.php

    <?php
    use yiihelpersHtml;
    use yiicaptchaCaptcha;
    echo $this->render('@app/views/public/testNav');
    ?>
    <?=Html::beginForm("",'post',['class'=>'forms'])?>
    <?=Captcha::widget([
        'model'=>$model, //Model
        'attribute'=>'verifyCode',//字段
        'captchaAction'=>'code/captcha',//验证码的action 与 Model 是对应的,code/captcha
        'template'=>'{input}{image}', //模版,可以自定义
        'options'=>[
            //input 的Html属性配置
            'class'=>'input verifycode',
            'id'=>'verifyCode'
        ],
        'imageOptions'=>[
            //image的Html属性
            'class'=>'imagecode',
            'alt'=>'点击图片刷新'
        ]
    ]);?>
    <?=Html::submitButton("提交",['class'=>'submit'])?>
    <?=Html::endForm();?>
    我生活的地方,我为何要生活。
  • 相关阅读:
    【IBM】netperf 与网络性能测量
    netperf 网络性能测试
    Netperf测试技巧
    网络测试工具--Iperf、Netperf 、MZ
    iozone
    iozone的使用与介绍-20191105
    XRDP freerdp
    性能工具 stream 最新版本5.10 The STREAM benchmark
    Linux学习之路-Linux-at及cron命令【7】---20171215
    centos7基于luks对磁盘进行加密
  • 原文地址:https://www.cnblogs.com/hsd1727728211/p/5913407.html
Copyright © 2011-2022 走看看