danny3b/recaptcha

轻松在 CakePHP 3.2+ 项目中使用 Google Recaptcha

安装: 19

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 18

类型:cakephp-plugin

2.1.0 2018-04-26 16:16 UTC

This package is auto-updated.

Last update: 2024-09-24 23:34:44 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

将 Google Recaptcha v2 集成到您的 CakePHP 项目中

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方法是

composer require crabstudio/recaptcha

或者在您的应用程序的 composer.json 文件中添加以下行

"require": {
    "crabstudio/recaptcha": "^2.0"
}

然后执行以下命令

composer update

加载插件

从命令行

bin/cake plugin load Recaptcha

或者将此行添加到 Your_project\config\bootstrap.php 文件的末尾

Plugin::load('Recaptcha');

加载组件并配置

从 loadComponent 覆盖默认配置

$this->loadComponent('Recaptcha.Recaptcha', [
    'enable' => true,     // true/false
    'sitekey' => 'your_site_key', //if you don't have, get one: https://www.google.com/recaptcha/intro/index.html
    'secret' => 'your_secret',
    'type' => 'image',  // image/audio
    'theme' => 'light', // light/dark
    'lang' => 'vi',      // default en
    'size' => 'normal'  // normal/compact
]);

用法

在您的视图中显示 recaptcha

    <?= $this->Form->create() ?>
    <?= $this->Form->control('email') ?>
    <?= $this->Recaptcha->display() ?>  // Display recaptcha box in your view, if configure enable = false, nothing to display here
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>

如果 recaptcha 成功,则在回调中启用按钮

    <?= $this->Form->button(__('Submit'), ['disabled']) ?>
    
    <script>
        function recaptcha_callback(){
            $('button[type="submit"]').prop("disabled", false);
        }
    </script>

在控制器函数中进行验证

    public function forgotPassword() {
        if ($this->request->is('post')) {
            if ($this->Recaptcha->verify()) { // if configure enable = false, always return true
                //do something here
            }
            $this->Flash->error(__('Please pass Google Recaptcha first'));
        }
    }

完成