Alessandro Corradini
Alessandro Corradini

Reputation: 499

Including and using Zend Service ReCaptcha in ZF2 (v2.3.3)

how to include Recaptcha service in zend framework 2?

I tried to do like this:

public function contactAction()
{
    $formContact = new ContactForm();
    $pubKey = 'mypubkey';
    $privKey = 'myprivkey';
    $recaptcha = new ZendService\ReCaptcha\ReCaptcha($pubKey, $privKey);
    return array ('formContact' => $formContact, 'recaptcha' => $recaptcha);

}

but I discovered that ZendService\ReCaptcha is not present by default when you download the framework. So, I downloaded it from here https://github.com/zendframework/ZendService_ReCaptcha

and I placed it into vendor\zendframework\zendframework\library\zend together with the other parts of the library.

I tried to refresh the page but doesn't work again because it can't find the zend service recaptcha.

Fatal error: Class 'Application\Controller\ZendService\ReCaptcha\ReCaptcha' not found in C:\Program Files (x86)\xampp\htdocs\Zf-tutorial\module\Application\src\Application\Controller\IndexController.php on line 79

can someone help me? I thought it was simple to implement recaptcha, but it is not so ! thanks!

Upvotes: 0

Views: 1948

Answers (3)

matwr
matwr

Reputation: 1571

Add the zendservice-recaptcha module to your composer.json file and run an update:

{
        ...
        "repositories": [
            {
                "type": "composer",
                "url": "http://packages.zendframework.com/"
            }
        ],
        ...
        "require": {
            ...
            "zendframework/zendservice-recaptcha": "*",
            ...
        }
        ...
    }

update composer :

php composer.phar update

This will install the module and configure the relevant class mapping and you will be able to access the classes by adding the use statements as with any other classes you use.

Upvotes: 2

user4281474
user4281474

Reputation: 1

You do not properly install the library ZendService\ReCaptcha.

your system write:

Class 'Application\Controller\ZendService\ReCaptcha\ReCaptcha' not found

You must:

  • placed it into vendor\zendframework\zendframework\library
  • In the file vendor/ZF2/library/Zend/Loader/StandardAutoloader.php insert string

    $this->registerNamespace('ZendService', dirname(dirname((__DIR__))) . '/ZendService');

    in case self::AUTOREGISTER_ZF:

  • in the file init_autoloader.php insert string

    $loader->add('ZendService', $zf2Path);.

Upvotes: -1

ankush madaan
ankush madaan

Reputation: 300

Even i tried recaptcha but with no success so implemented something different to refresh captcha and worked very well, try this once

resetCaptcha function:

    $form = $this->getServiceLocator()->get('zfcuser_register_form');
    $captcha = $form->get('captcha')->getCaptcha();
    $data = array();
    $data['id'] = $captcha->generate();
    $data['src'] = $captcha->getImgUrl() .
    $captcha->getId() .
    $captcha->getSuffix();
    return $data;

ajax request :

$(document).ready(function() {
        $('#refreshcaptcha').click(function() {
            var data = [];
            var form = <?php $this->registerForm; ?>
            data.push({name: "action", value: 'resetCaptcha'});
            data.push({name: "params[form]", value: form});
            $.post("<?php echo BASE_URL ?>/user/iajax", data,
               function(data) {
                        $('#form_reg img').attr('src', data.src);
                        $('#captcha-id-hidden').attr('value', data.id);
                    }, 'json');
        });
    });

Html call :

   <p class="refresh_captcha"><?php echo $this->formCaptcha($form->get('captcha')); ?>
                <input type="button" id="refreshcaptcha" value="refresh">
            </p>

Upvotes: 0

Related Questions