crabstudio/recaptcha

轻松在 CakePHP 项目中使用 Google Recaptcha

安装次数: 87,849

依赖关系: 2

建议者: 4

安全性: 0

星标: 21

关注者: 4

分支: 17

类型:cakephp-plugin

4.0.0 2023-10-18 18:25 UTC

This package is auto-updated.

Last update: 2024-09-18 20:17:45 UTC


README

Build Status Latest Stable Version Total Downloads License

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

安装

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

安装 composer 包的推荐方式是

composer require crabstudio/recaptcha

然后执行以下命令

composer update

加载插件

从命令行加载

bin/cake 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
]);

从应用程序配置文件中覆盖默认配置

file: config/app.php

    /**
     * Recaptcha configuration.
     *
     */
    'Recaptcha' => [
        'enable' => true,
        'sitekey' => 'your_site_key',
        'secret' => 'your_secret',
        'type' => 'image',
        'theme' => 'light',
        'lang' => 'es',
        'size' => 'normal'
    ]

从 recaptcha 配置文件中覆盖默认配置

file: config/recaptcha.php

<?php

return [
    /**
     * Recaptcha configuration.
     *
     */
    'Recaptcha' => [
        'enable' => true,
        'sitekey' => 'your_site_key',
        'secret' => 'your_secret',
        'type' => 'image',
        'theme' => 'light',
        'lang' => 'es',
        'size' => 'normal'
    ]
];

加载 recaptcha 配置文件

file: config/bootstrap.php

    Configure::load('recaptcha', 'default', true);

配置偏好

  1. loadComponent 配置选项
  2. recaptcha 配置文件
  3. 应用程序配置文件

用法

在视图中显示 recaptcha

    <?= $this->Form->create() ?>
    <?= $this->Form->control('email') ?>
    <?= $this->Recaptcha->display() ?>  // Display recaptcha box in your view, if configure has enable = false, nothing will be displayed
    <?= $this->Form->button() ?>
    <?= $this->Form->end() ?>

在控制器函数中验证

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

完成