首页 > 其他分享 >Yii2 Action用法

Yii2 Action用法

时间:2024-03-30 15:00:15浏览次数:20  
标签:public yii actions 用法 captcha test Action Yii2 class

Yii2 Action用法

actions方法

public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

自定义Action

创建TestAction类 common\action\TestAction.php

<?php

namespace app\common\action;

use yii\base\Action;

class TestAction extends Action{
    public $name;
    public $age;

    public function run(){
        echo 'run test action name: '.$this->name.' age: ' .$this->age;
    }
}

调用

   public function actions()
    {
        return [
            'test'=>[
                'class'=>'app\common\action\TestAction',
                'name'=>'胡勇健',
                'age'=>25
            ]
        ];
    }

访问

http://www.yii2.com/test/test

errorAction

actions方法添加error

 public function actions()
    {
        return [
            'error'=>[
                'class'=> 'yii\web\ErrorAction'
            ]
        ];
    }

添加view

<?php
use yii\helpers\Html;

$this->title = $name;
?>
<div class="site-error">

    <h1><?= Html::encode($this->title) ?></h1>

    <div class="alert alert-danger">
        <?= nl2br(Html::encode($message)) ?>
    </div>

    <p>
       请联系管理员.
    </p>

</div>

修改配置 config\web.php

'errorHandler' => [
            'errorAction' => 'test/error',
        ],

CaptchaAction 验证码

controllers\TestController.php actions方法添加captcha操作

public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

生成验证码

http://www.yii2.com/test/captcha

验证方法

    $code = 'pedone';
    $captcha = new CaptchaValidator();
    $captcha->captchaAction = 'test/captcha';
    $bool = $captcha->validate($code);
    var_dump($bool);

标签:public,yii,actions,用法,captcha,test,Action,Yii2,class
From: https://www.cnblogs.com/hu308830232/p/18105502

相关文章

  • yii2 密码和加解密
    yii2密码和加解密生成hash$hash=Yii::$app->getSecurity()->generatePasswordHash($password);验证hashif(Yii::$app->getSecurity()->validatePassword($password,$hash)){//allgood,logginguserin}else{//wrongpassword}生成随机字符串$k......
  • yii2-分页
    yii2-分页使用ActiveDataProvider和GridViewcontrollerpublicfunctionactionIndex(){$dataProvider=newActiveDataProvider(['query'=>ArticleClass::find(),'pagination'=>['......
  • Yii2-助手类(StringHelper)
    Yii2-助手(StringHelper)截断字符串echoStringHelper::truncate('中文截断字符',4);//'中文截断...'字符串转数组StringHelper::explode('thisisstring','');//[0=>'this'1=>'is'2=>'string......
  • Yii2-助手类(Html)
    Yii2-助手类(Html)引入命名空间useyii\helpers\Html;使用<?php//【一】表单:Html::beginForm(提交地址,提交方法,属性数组);?><?=Html::beginForm('','post',['id'=>'form','class'=>'form','data'=&g......
  • yii2 小部件
    yii2小部件小部件基本上在views中使用,在视图中可调用yii\base\Widget::widget() 方法使用小部件。继承yii\base\Widget类并覆盖yii\base\Widget::init() 和/或yii\base\Widget::run() 方法可创建小部件。创建小部件<?phpnamespaceapp\widgets;useyii\base\Wid......
  • yii2依赖注入
    yii2依赖注入所谓依赖注入,实质上就是当某个类对象需要使用另一个类实例的时候,不在类内部实例化另一个类,而将实例化的过程放在类外面实现,实例化完成后再赋值给类对象的某个属性。 这样的话该类不需要知道赋值给它的属性的对象具体属于哪个类的,当需要改变这个属性的类型的时候,无需......
  • yii2事件
    yii2事件事件可以将自定义代码“注入”到现有代码中的特定执行点。1yii\base\Event封装了与时间相关的有关数据并提供了一些功能函数作为辅助classEventextendsObject{public$name;//事件名public$sender;//事件发布者,通常是......
  • Yii2验证器Validator
    Yii2验证器Validator控制器publicfunctionactionIndex(){$data=['LoginForm'=>['username'=>'huyongjian','password'=>'1234']];$model=newLoginForm();$model->load($......
  • yii2-队列使用
    yii2-队列使用安装yii2-queuecomposerrequireyiisoft/yii2-queue安装yii2-rediscomposerrequireyiisoft/yii2-redis控制台配置文件console.php和web.php'redis'=>['class'=>'yii\redis\Connection',&......
  • Yii2 过滤器
    Yii2过滤器过滤器AccessControllerAccessControl基于一组规则提供简单的访问控制/***{@inheritdoc}*/publicfunctionbehaviors(){return['access'=>['class'=>AccessControl::class,......