astritzeqiri / g-recaptcha
轻松添加 Google reCAPTCHA
1.0.3
2016-08-31 09:33 UTC
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~5.3|~6.0
- laravel/framework: 5.*
This package is not auto-updated.
Last update: 2024-09-14 19:47:33 UTC
README
Google reCAPTCHA for Laravel
轻松在 Laravel 中添加 Google reCAPTCHA。
要求
- PHP >=5.4
- Laravel >= 5.0
- GuzzleHttp >= 5.3
安装
将 astritzeqiri/g-recaptcha 添加到您的 composer.json 文件中
"require": { "astritzeqiri/g-recaptcha": "~1.0" }
然后执行
$ composer update
或者让 composer 安装该包
$ composer require astritzeqiri/g-recaptcha
配置
现在您前往下面的链接生成新的 Google reCAPTCHA
然后您需要进入 .env 文件并设置以下变量
# If you want to disable captchas put this to false by default this is true.
GRECAPTCHA_ENABLED=true
# The google recaptcha site key
GRECAPTCHA_KEY=SITE_KEY
# The google recaptcha secret key
GRECAPTCHA_SECRET=SECRET_KEY
您也可以在 config/grecaptcha.php 文件上更改这些变量。
return [ 'enabled' => env("GRECAPTCHA_ENABLED", true), 'site_key' => env("GRECAPTCHA_KEY"), 'secret_key' => env("GRECAPTCHA_SECRET"), ];
注册包
在 app/config/app.php 中的 providers 数组内注册服务提供者
'providers' => array( // ... AstritZeqiri\GRecaptcha\GRecaptchaServiceProvider::class )
然后您需要在 app/config/app.php 中的 aliases 数组内添加 GRecaptcha 类
'aliases' => array( // ... 'GRecaptcha' => AstritZeqiri\GRecaptcha\GRecaptcha::class, )
然后运行 php artisan vendor:publish 以发布 start_captchas 脚本以及 reCAPTCHA 配置文件。
$ php artisan vendor:publish
用法
首先您需要在页脚中调用 grecaptcha 脚本。这些脚本只有在您的 HTML 中有活动的 reCAPTCHA 时才会渲染。
// in blade.php files {!! \GRecaptcha::renderScripts() !!} // or in .php files <?php echo GRecaptcha::renderScripts(); ?>
基本示例
现在要渲染新的 GRecaptcha,请调用 render 方法。
// by default it echo's it out GRecaptcha::render(); // if you want to save the html in a variable you call $grecaptchaHtml = GRecaptcha::render([], false);
如果您想要获取一个新的 reCAPTCHA 实例
$grecaptcha = GRecaptcha::generate(); // to render it you call $grecaptcha->renderHtml(); // if you dont want it to be rendered but store the html you call $grecaptchaHtml = $grecaptcha->build();
验证
当您验证表单以验证 reCAPTCHA 时,您使用规则 grecaptcha
$validator = Validator::make($inputs, ['g-recaptcha-response' => 'required|grecaptcha'] );