Search code examples
formszend-framework2recaptchazend-form2

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!


Solution

  • 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.