Zend Framework 2:如何在zfcUser上用reCaptcha正确替换Figlet [英] Zend Framework 2: How to properly replace Figlet with reCaptcha on zfcUser

查看:126
本文介绍了Zend Framework 2:如何在zfcUser上用reCaptcha正确替换Figlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用zfcUser注册表格上的reCaptcha替换Figlet.有关如何完成此操作的部分说明,可以在 https://github上找到. com/ZF-Commons/ZfcUser#changing-registration-captcha-element ,但不存在完整的说明.

I am trying to replace Figlet with reCaptcha on a zfcUser registration form. Partial instruction on how to accomplish this can be found on https://github.com/ZF-Commons/ZfcUser#changing-registration-captcha-element but no complete instruction exists.

检查README.md文件具有如何完成此操作的两步说明,但是验证码仍会在表单上呈现时使用Figlet.

Checking the README.md file has a two-step instruction on how to accomplish this but still the CAPTCHA uses Figlet when rendered on the form.

有人成功实现了吗?我真的需要帮助.

Has anyone successfully implemented this? I really need a hand on this one.

谢谢.

1.添加到composer.json

// Add the lines below under the "require" element:
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": ">2.2.0rc1",
        "zendframework/zendservice-recaptcha": "2.*"

}

2.转到项目的ZF2安装目录并执行以下命令:

php composer.phar update

3.将config/autoload/database.global.php替换或创建为:

<?php
$config = array(
    'dbdriver' => 'pdo',
    'dbhost' => 'localhost',
    'dbport' => '3306',
    'dbname' => 'CHANGEME',
    'dbuser' => 'CHANGEME',
    'dbpass' => 'CHANGEME',
);

return array(
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
    'db' => array(
        'driver'    => 'pdo',
        'dsn'       => 'mysql:dbname='.$config['dbname'].';host='.$config['dbhost'],
        'username'  => $config['dbuser'],
        'password'  => $config['dbpass'],
    ),
);

4:在您的mySQL服务器上执行此操作:

CREATE TABLE `user`
(
    `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `username` VARCHAR(255) DEFAULT NULL UNIQUE,
    `email` VARCHAR(255) DEFAULT NULL UNIQUE,
    `display_name` VARCHAR(50) DEFAULT NULL,
    `password` VARCHAR(128) NOT NULL,
    `state` SMALLINT UNSIGNED
) ENGINE=InnoDB CHARSET="utf8";

5.使用以下命令创建/替换config/autoload/recaptcha.global.php:

<?php
define('RECAPTCHA_PRIVATE_KEY','CHANGEME');
define('RECAPTCHA_PUBLIC_KEY','CHANGEME');

return array(
    'zfcuser' => array(
        'form_captcha_options' => array(
            'class'   => 'Zend\Captcha\ReCaptcha',
            'options' => array(
                'privkey' => RECAPTCHA_PRIVATE_KEY,
                'pubkey'  => RECAPTCHA_PUBLIC_KEY,
            ),
        ),
    ),

    'di'=> array(
        'instance'=>array(
            'alias'=>array(
                'recaptcha_element' => 'Zend\Form\Element\Captcha',
            ),

            'ZfcUser\Form\Register' => array(
                'parameters' => array(
                    'captcha_element'=>'recaptcha_element',
                ),
            ),
        ),
    ),
);

6.使用以下命令创建/替换config/autoload/zfcuser.global.php:

<?php
$settings = array(    
    'enable_registration' => true,
    'enable_username' => true,
    'auth_adapters' => array( 100 => 'ZfcUser\Authentication\Adapter\Db' ),
    'enable_display_name' => false,
    'auth_identity_fields' => array( 'email' ),
    'use_registration_form_captcha' => true,
    'user_login_widget_view_template' => 'zfc-user/user/login.phtml',
);


return array(
    'zfcuser' => $settings,
    'service_manager' => array(
        'aliases' => array(
            'zfcuser_zend_db_adapter' => (isset($settings['zend_db_adapter'])) ? $settings['zend_db_adapter']: 'Zend\Db\Adapter\Adapter',
        ),
    ),
);

7.导航到 http://yourdomain.com/user

7. Navigate to http://yourdomain.com/user

8.享受! :)

推荐答案

这是我的操作方式,可能不是最好或正确的方法,但对我有用:

This is how I did it, it might not be the best or correct way but it worked for me:

将Recaptcha服务添加到您的composer.json文件:

Add the recaptcha service to your composer.json file:

"require": {
    "Zendframework/zendservice-recaptcha": "2.*"
}

运行作曲家以获取服务.然后,您需要指定ReCaptcha配置. 我创建了一个单独的配置文件来存储ReCaptcha密钥:

Run composer to get the service. Then you need to specify the ReCaptcha config. I created a separate config file to store the ReCaptcha keys:

//zfcuser.local.php
return array(
    'zfcuser' => array(
        'form_captcha_options' => array(
            'options' => array(
                'privkey' => RECAPTCHA_PRIVATE_KEY,   
                'pubkey'  => RECAPTCHA_PUBLIC_KEY,
            ),
        ),
    ),
); 

然后ZfcUser验证码配置看起来像这样,告诉它使用ReCaptcha服务:

Then ZfcUser captcha config looks like so, telling it to use the ReCaptcha service:

//zfcuser.global.php
'form_captcha_options' => array(
    'class'   => 'Zend\Captcha\ReCaptcha',
    'options' => array(
        'wordLen'    => 6,
        'expiration' => 300,
        'timeout'    => 300,
     ),
),

您不需要recaptcha.global.php.您可以随意调用配置文件,只要该文件以.global.php或.local.php结尾即可.在不需要版本控制的情况下,通常将其命名为.local.php.

You don't need the recaptcha.global.php. You can call the config file whatever you like aslong as it ends with .global.php or .local.php. You usually name things .local.php when you don't want them in version control.

在这种情况下,我将文件命名为zfcuser.local.php,因为它所做的只是存储ReCaptcha密钥,而我不想在版本控制中使用它们.

In this case I named the file zfcuser.local.php because all it does is store the ReCaptcha keys and I didn't want them in version control.

启动应用程序时,所有配置文件都将合并到一个阵列中.因此,基本上,请忽略ZfcUser文档.或者,也许其他人可以解释如何使其以这种方式工作.

All the config files get merged in to one array when the application is started. So basically, ignore the ZfcUser documentation. Or maybe someone else can explain how to get it working that way.

第三段代码是zfcuser.global.php.

The third block of code is the zfcuser.global.php.

这篇关于Zend Framework 2:如何在zfcUser上用reCaptcha正确替换Figlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆