Detailed explanation of how to add verification code in modules in Yii2

怪我咯
Release: 2023-03-12 15:46:02
Original
1455 people have browsed it

This article mainly introduces how to add verification code in Yii2 modules. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

I recently played with the verification code part of Yii2. Normal logic can be used. There are no problems in the online examples. The key problematic part is in the module. When using it, share it with everyone. Before reading further, you can see how it is used under normal circumstances.

The code in the controller part is similar to the one on the Internet.

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

The code in the model part [something to note here]

public function rules()
{
  return [
    ['username', 'required', 'message' => '登录账号不能为空'],
    ['password', 'required', 'message' => '登录密码不能为空'],
    ['verifyCode', 'required', 'message' => '验证码不能为空'],
    ['verifyCode', 'captcha', 'captchaAction' => 'admin/default/captcha', 'message' => '验证码输入错误'],
    ['rememberMe', 'boolean'],
    ['password', 'validatePassword'],
  ];
}
Copy after login

verifyCode in rules needs to be added. A value corresponding to captchaAction, otherwise the verification code verification will not pass, and the number of the verification code will not change. The reason should be that the

view part of the code is caused by the default use of site/captcha [due to php The mixed layout with HTML makes me unable to tolerate the chaotic layout of the page style, so I try to take out the parameter configuration part]

$captchaConfig = [
  'name' => 'captchaimg',
  'captchaAction' => ['/admin/default/captcha'],
  &#39;template&#39; => &#39;<p class="form-group"><p>{image}</p></p>&#39;,
  &#39;imageOptions&#39; => [
    &#39;id&#39; => &#39;captchaimg&#39;,
    &#39;title&#39; => &#39;换一个&#39;,
    &#39;alt&#39; => &#39;换一个&#39;,
    &#39;style&#39; => &#39;cursor:pointer;margin-left:25px;&#39;,
  ],
];
Copy after login
<?=Captcha::widget($captchaConfig);?>
Copy after login

The above is the detailed content of Detailed explanation of how to add verification code in modules in Yii2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!