cakephp-fr / recaptcha
轻松在 CakePHP 项目中使用 Google Recaptcha(免费 CAPTCHA 服务,可保护网站免受垃圾邮件和滥用)
README
请注意,版本 1.0 适用于 CakePHP 3.6 及以上版本。对于之前的 CakePHP 版本,请使用 0.4.2 版本。
插件目标
此插件为 CakePHP 项目添加了使用新的 reCAPTCHA API 版本 2.0 的功能。
此插件仍在开发中... 目前,单个页面上的多个小部件尚不可用。
要求
- PHP >= 5.4.16
- CakePHP 3.x
- 服务器名称为
localhost
。请注意,如果您有名为 local.dev/dev/ 的虚拟主机,则小部件将不会显示。
安装
[使用 Composer]
将插件添加到项目的 composer.json
文件中 - 类似于以下内容
composer require cakephp-fr/recaptcha:~1.0
然后,您需要运行以下命令来加载插件
bin/cake plugin load -rb Recaptcha
您可以在 config/boostrap.php
文件的底部检查是否已创建以下行:Plugin::load('Recaptcha', ['routes' => true, 'bootstrap' => true]);
在生产环境中,应该删除 'routes' => true
。如果您想查看演示,则该选项才有效。
插件使用方法
1. 访问 Google reCAPTCHA 网站
请点击此处为您的网站创建密钥对。
2. 配置插件
最简单的方法是将 recaptcha 配置添加到 config/app.php
文件中,例如
return [ .... (other configs before) 'Recaptcha' => [ // Register API keys at https://www.google.com/recaptcha/admin 'sitekey' => 'your-sitekey', 'secret' => 'your-secret', // reCAPTCHA supported 40+ languages listed // here: https://developers.google.com/recaptcha/docs/language 'lang' => 'en', // either light or dark 'theme' => 'light', // either image or audio 'type' => 'image', // either normal or compact 'size' => 'normal' ] ]
请确保 /config/app.php
文件包含在 .gitignore
中。密钥必须是保密的。
如果您没有密钥和密钥,将引发异常。
3. 然后,在需要 reCAPTCHA 的控制器中添加组件。
例如
public function initialize() { parent::initialize(); if ($this->request->getParam('action') === 'contact') { $this->loadComponent('Recaptcha.Recaptcha'); } }
public function contact() { if ($this->request->is('post')) { if ($this->Recaptcha->verify()) { // Here you can validate your data if (!empty($this->request->getData())) { $this->Flash->success(__('We will get back to you soon.')); return $this->redirect($this->referer()); } else { $this->Flash->error(__('There was a problem submitting your form.')); } } else { // You can debug developers errors with // debug($this->Recaptcha->errors()); $this->Flash->error(__('Please check your Recaptcha Box.')); } } }
4. 最后,在表单模板中添加 <?= $this->Recaptcha->display() ?>
。
无需 添加辅助程序:它将与组件一起添加。
例如
<?= $this->Form->create() ?> <?= $this->Form->control('name', [ 'label' => __('Your Name'), // 'default' => $this->request->query('name'); // in case you add the Prg Component ]) ?> <?= $this->Form->control('message', [ 'type' => 'textarea', // 'default' => $this->request->query('message'); // in case you add the Prg Component 'label' => __('Your Message') ]) ?> <?= $this->Recaptcha->display() ?> <?= $this->Form->button(__('OK')) ?> <?= $this->Form->end() ?>
请参阅 src/Controller/ContactController.php
、src/Template/Contact/index.ctp
和 src/Form/ContactForm.php
中的无表单联系人示例。您可以通过访问 https:///recaptcha/contact
来测试它。
里面有什么?
组件
- RecaptchaComponent
辅助程序
- RecaptchaHelper(当添加 RecaptchaComponent 时自动添加)
示例
- 控制器:ContactController
- 表单:ContactForm
- 模板:Contact/index.ctp
测试
要测试插件,请克隆它并运行 composer install
。然后运行
./vendor/bin/phpunit ./vendor/bin/phpcs -n -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests --ignore=vendor
支持和贡献
有关支持和功能请求,请通过 Github 问题与我联系
请随时通过新问题、请求、单元测试和代码修复或新功能为插件做出贡献。如果您想贡献代码,请创建功能分支,并发送您的拉取请求。新功能和检测到的问题的单元测试是保持高质量所必需的。
许可证
版权所有 (c) [2014-2018] [cakephp-fr]
本软件及其相关文档文件(以下简称“软件”)的副本(任何获得副本的人)有权免费使用软件,不受限制地处理软件,包括但不限于使用、复制、修改、合并、发布、分发、许可和/或销售软件副本,并允许软件提供者进行此类操作,但需遵守以下条件:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“现状”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任承担责任,无论该责任是基于合同、侵权或其他原因,无论该索赔、损害或其他责任是否因软件或软件的使用或其他操作而引起。