mikemix/mxrecaptcha

该包已被放弃且不再维护。没有建议的替代包。

ZF2 表单与 v2.0 reCaptcha 集成

1.2.0 2015-09-10 11:53 UTC

This package is auto-updated.

Last update: 2019-08-19 07:04:38 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

轻松实现 ZF2 表单与 reCaptcha v2.0 集成

安装

  1. 使用 Composer 安装 php composer.phar require 'mikemix/mxrecaptcha:~1.0'(我们遵循 语义版本控制 规则)
  2. 在您的 config/application.config.php 文件中加载 mxreCaptcha 模块
  3. 复制 dist 配置文件 cp vendor/mikemix/mxrecaptcha/config/mxrecaptcha.local.php.dist config/autoload/mxrecaptcha.local.php 并在其中写入您的私钥和公钥
  4. 您已经准备好了!

使用方法

首先,将 reCaptcha 元素添加到您的表单中。以下是一个示例表单类

namespace App\Form;

use Zend\Form\Form;

class AddForm extends Form
{
    public function init()
    {
        $this->add([
            'name' => 'recaptcha',   // or any name of your choice
            'type' => 'mxreCaptcha', // this is important, use our reCaptcha component
            'options' => [
                'label' => 'Prove you are human',
            ],
        ]);
        
        // or
        
        // This element behaves as any other element.
        // Aside from that, you can pass custom grecaptcha.render parameters
        // available at the docs
        // https://developers.google.com/recaptcha/docs/display#render_param
        // by setting widget_options key in the options, for example:
        
        $this->add([
            'name' => 'recaptcha',
            'type' => 'mxreCaptcha',
            'options' => [
                'label' => 'Prove you are human',
                'widget_options' => [
                    'theme' => 'dark',
                ],
            ],
        ]);
        
        $this->add([
            'name' => 'submit',
            'type' => 'submit',
        ]);
    }
}

示例控制器

namespace App\Controller;

use App\Form\AddForm;
use Zend\Mvc\Controller\AbstractActionController;

class FormController extends AbstractActionController
{
    public function indexAction()
    {
        $form = $this->getServiceLocator()->get('FormElementManager')
            ->get(AddForm::class);
        
        if ($this->request->isPost()) {
            $form->setData($this->request->getPost());
            
            if ($form->isValid()) {
                $this->flashMessenger()->addSuccessMessage('Success!');
                return $this->redirect()->toRoute('home');
            }
        }
        
        return [
            'form' => $form
        ];
    }
}

示例视图

<?php
/** @var App\Form\AddForm $form */
$form = $this->form->prepare();
?>

<?= $this->form()->openTag($form) ?>
<?= $this->formCollection($form) ?>
<?= $this->form()->closeTag() ?>

本地化

默认情况下,本地化错误消息的波兰语翻译已经可用。要为您的应用程序创建自定义翻译,只需将 pl.po 文件从 language 目录复制出来,并按照您的翻译器 locale 设置命名(例如 fr_ca.po)。最后,翻译错误消息并将此文件编译为 *.mo。将这些两个文件放入 language 目录中。

如果您愿意分享您的翻译,请发起一个 pull request。

单元测试

此模块附带单元测试。运行测试套件需要 phpUnit

  1. 克隆此存储库 git clone https://github.com/mikemix/mxreCaptcha.git
  2. 导航到它 cd mxreCaptcha
  3. 下载 composer php -r "readfile('https://getcomposer.org.cn/installer');" | php
  4. 安装依赖 php composer.phar update
  5. 运行套件 phpunit