klintlili / yii2-captcha
Captcha library wrapper for Yii2
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
pkg:composer/klintlili/yii2-captcha
Requires
- gregwar/captcha: 1.*
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2025-10-19 13:00:47 UTC
README
Gregwar's Captcha library wrapper for Yii2.
Installation
The preferred way to install this extension is through composer. Either run
php composer.phar require --prefer-dist klintlili/yii2-captcha "*"
or add
"klintlili/yii2-captcha": "*"
to the require section of your composer.json file.
Usage
This extension has 3 different steps. First is calling CaptchaAction to generate CAPTCHA image, then rendering CAPTCHA image in view, and validating user input against the generated CAPTCHA code.
Here is how to setup this extension for each step :
Action
Add the following method into your Controller.
public function actions() { return [ 'captcha' => [ 'class' => 'klintlili\captcha\CaptchaAction', //'length' => 5, // captcha character count //'width' => 150, // width of generated captcha image //'height' => 40, // height of generated captcha image ], ]; }
Some configurable attributes are :
lengthAn integer value to set the generated CAPTCHA character countwidthAn integer value to set the width of generated CAPTCHA imageheightAn integer value to set the height of generated CAPTCHA image
View file
Add the following code to your view to render CAPTCHA image and input.
use klintlili\captcha\Captcha; ... <?php echo Captcha::widget([ 'model' => $model, 'attribute' => 'captcha', //'captchaAction' => 'site/captcha', // captcha action, default to site/captcha //'template' => '{image} {input}', // template for rendering CAPTCHA image and input //'options' => [ // HTML attribute for rendering text input // 'class' => 'form-control', //], ]) ?>
You can also use ActiveForm instance to render CAPTCHA input.
use klintlili\captcha\Captcha; ... <?php echo $form->field($model, 'captcha')->widget(Captcha::className()) ?>
Some configurable attributes are :
captchaActionCaptcha action, default tosite/captchatemplateTemplate for rendering CAPTCHA image and input. In this template, the token{image}will be replaced with the actual image tag, while{input}will be replaced with the text input tag.optionsthe HTML attributes for the input tag.
Validation
Add the following rule to your model to validate the captcha input :
use klintlili\captcha\CaptchaValidator; ... public function rules() { return [ ... some other rules... ['captcha', CaptchaValidator::className()], ]; }