Simple Captcha for Laravel 4
Installation
{
"require": {
"laravel/framework": "4.0.*",
"unodepiera/simplecaptcha": "dev-master"
},
"minimum-stability": "dev"
}
Update your packages with composer update
or install with composer install
., (*1)
Usage
Find the providers key in app/config/app.php and register the Captcha Service Provider., (*2)
'providers' => array(
//...
'Unodepiera\Simplecaptcha\SimplecaptchaServiceProvider',
)
Find the aliases key in app/config/app.php., (*3)
'aliases' => array(
//...
'Simplecaptcha' => 'Unodepiera\Simplecaptcha\Facades\Simplecaptcha',
)
Publish assets with this command., (*4)
$ php artisan asset:publish unodepiera/simplecaptcha
, (*5)
Options captcha
You can set the following options for the captcha., (*6)
$defaultOptions = array(
"font" => "PT_Sans-Web-Regular.ttf",
"width" => 250,
"height" => 140,
"font_size" => 25,
"length" => 6,
"num_lines" => "",
"num_circles" => "",
"text" => "",
"expiration" => 600,
"directory" => "packages/unodepiera/simplecaptcha/captcha/",
"dir_fonts" => "packages/unodepiera/simplecaptcha/fonts/",
"type" => "alpha",
"bg_color" => "181,181,181",
"border_color" => "0,0,0"
);
- Font: type font for captcha, view folder public/fonts.
- Num_lines: number lines you would for captcha.
- Num_circles: number circles you would for captcha.
- Expiration: number of seconds it will take to be removed captchas.
- Directory: folder on save captchas.
- Dir_fonts: directory on save the fonts.
- Type: alpha or alphanum.
Example Usage
Route::get("form", function()
{
$options = array(
"width" => 280,
"height" => 100,
"font_size" => 28,
"length" => 8,
"num_circles" => 0,
"num_lines" => 4,
"expiration" => 600,
"bg_color" => "20,20,20"
);
$captcha = Simplecaptcha::captcha($options);
return View::make("form", array("captcha" => $captcha));
});
Route::post("process", function()
{
$rules = array('captcha' => array('required', 'captcha'));
$validator = Validator::make(Input::all(), $rules);
if($validator->fails())
{
echo "fails";
}else{
echo "success";
}
});
Now you can use the captcha in the view as follows:, (*7)
{{ Form::open(array('url' => 'process')) }}
|
{{ $captcha["img"] }}
|
{{ Form::label('captcha', 'Captcha') }}
|
{{ Form::text('captcha') }}
|
|
{{ Form::submit('Success') }}
|
{{ Form::close() }}
Visit me