cake17 / cakephp-recaptcha
[已废弃] 请使用 https://github.com/cakephp-fr/recaptcha/
README
此插件仍在开发中...
插件目标
此插件为 CakePHP 项目添加使用新 Google reCAPTCHA 的功能。目前不支持单页上的多个小部件。
需求
- PHP >= 5.4.16
- CakePHP 3.x
- 服务器名称为
localhost
。请注意,如果您有名为 local.dev/dev/ 的 vhost,则小部件将不会显示。
安装
[使用 Composer]
将插件添加到项目的 composer.json
文件中 - 例如:
{ "require": { "cake17/cakephp-recaptcha": "dev-master" } }
然后运行 composer update
。
由于此插件在其自己的 composer.json
中设置了类型 cakephp-plugin
,因此 Composer 知道将其安装到您的 /vendor
目录中。建议您将 /vendor
添加到 .gitignore 文件中。(为什么?阅读此内容。)
插件使用方法
1. 启用插件
在您的 config/bootstrap.php
文件中
Plugin::load('Recaptcha', ['routes' => true, 'bootstrap' => true]);
2. 前往 Google reCAPTCHA 网站
前往 此处 为您的网站创建一对密钥。
3. 创建或复制 reCAPTCHA 配置文件
-
可以从
/config/recaptcha.php
中的/vendor/cake17/cakephp-recaptcha/config/recaptcha.default.php
复制默认文件。 -
或者使用 composer install 命令在 composer.json 中添加将为您进行复制的代码。要使用它,请将以下片段添加到您的项目 composer.json 中,并在之后运行
composer run-script post-install-cmd
:... "scripts": { "post-install-cmd": [ "Recaptcha\\Console\\Installer::postInstall" ] } ...
无论您使用何种方法复制 reCAPTCHA 配置文件,都别忘了将 /config/recaptcha.php
文件添加到 .gitignore。
4. 在 /config/recaptcha.php
中填写信息
- sitekey:在 Google 网站上获取
- secret:在 Google 网站上获取
- lang:在 Google 网站上查看列表
- theme:暗或亮
- type:图片或音频
如果您没有密钥和密钥,将引发异常。
5. 然后在需要 reCAPTCHA 的控制器中添加组件。
例如
public function initialize() {
parent::initialize();
if ($this->request->action === 'contact') {
$this->loadComponent('Recaptcha.Recaptcha');
}
}
6. 在您的控制器中添加以下内容。
public function contact() {
if ($this->request->is('post')) {
if ($this->Recaptcha->verify()) {
if ($contact->execute($this->request->data)) {
$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 {
$this->Flash->error(__('Please check your Recaptcha Box.'));
}
}
}
7. 不需要添加助手。
它将与组件一起添加。
8. 最后,在表单模板中添加 <?= $this->Recaptcha->display() ?>
。
例如
<?= $this->Form->create() ?>
<?= $this->Recaptcha->display() ?>
<?= $this->Form->input('name', [
'label' => __('Your Name'),
// 'default' => $this->request->query('name'); // in case you add the Prg Component
]) ?>
<?= $this->Form->input('message', [
'type' => 'textarea',
// 'default' => $this->request->query('message'); // in case you add the Prg Component
'label' => __('Your Message')
]) ?>
<?= $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 问题与我联系
请随时为新插件贡献新问题、请求、单元测试和代码修复或新功能。如果您想贡献代码,请创建一个功能分支,并向我们发送您的pull请求。为新的功能和检测到的问题编写单元测试是保持高质量所必需的。
许可证
版权所有(c)[2014-2015] [cake17]
特此免费授予任何获得本软件及其相关文档文件(以下简称“软件”)副本的个人,无限制地使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许获得软件的个人这样做,但受以下条件约束
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是在合同行为、侵权行为或其他行为中,源于、出于或与软件或软件的使用或其他交易有关。