Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Recaptcha Bundle Laravel Package

dmishh/recaptcha-bundle

View on GitHub
Deep Wiki
Context7

reCAPTCHA support for Symfony2

Features

  • Easy-to-use
  • Service-oriented architecture
  • Integration into forms
  • Integration into Security Component — for protecting login form with reCAPTCHA
  • Independent reCAPTCHA API realization (Recaptcher by default)
  • Both, PHP and Twig template engines support

Installation using Composer

  1. Add the following to your composer.json file:

    // composer.json
    {
        // ...
        "require": {
            // ...
            "dmishh/recaptcha-bundle": "1.0.*"
        }
    }
    
  2. Update dependencies, run from command line:

    php composer.phar update
    
  3. Register the bundle in your AppKernel.php file:

    <?php
    
    // in AppKernel::registerBundles()
    $bundles = array(
        // ...
        new Dmishh\Bundle\RecaptchaBundle\RecaptchaBundle()
    );
    

Configuration

Your reCAPTCHA's public and private keys that can be found at your recaptcha admin page. Configure RecaptchaBundle in your config.yml:

# ...

recaptcha:
    public_key: 6LeJg90SAAAAAA9yk0zeNrF8QKaxqR_bV_9SNLz9
    private_key: 6LeJg90SAAAAAEuTLEbZuymhkigzzPm2_wsSdA8j
    use_https: false # optional

Usage in forms

Add the following line to create the reCAPTCHA field:

<?php

// your form ...

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', 'recaptcha');
}

You can pass extra options to reCAPTCHA with the widget_options option:

<?php

// your form ...

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', 'recaptcha', array(
        'widget_options' => array(
            'theme' => 'clean'
        )
    ));
}

List of valid options:

  • theme
  • lang
  • custom_translations
  • custom_theme_widget
  • tabindex

Visit Customizing the Look and Feel of reCAPTCHA for the details of customization.

Validation

RecaptchaType has built-in validator, you don't need to do anything more. Just use it!

But if you need to disable validation for some reasons, then remove existing validator:

<?php

// your form ...

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', 'recaptcha', array(
        // only for disabling validation
        'constraints'   => array()
    ));
}

Usage in login form

You need to define RecaptchaFormAuthenticationListener as default listener for form authentication in services.yml:

# ...

security.authentication.listener.form:
    class: Dmishh\Bundle\RecaptchaBundle\Security\Firewall\RecaptchaFormAuthenticationListener
    parent: security.authentication.listener.abstract
    calls:
        - [setRecaptcha, [@recaptcha]]
    abstract: true

Second parameter in setRecaptcha method is used for disabling validator. This might be useful when you need to disable check in dev environment. For example:

security.authentication.listener.form:
    class: Dmishh\Bundle\RecaptchaBundle\Security\Firewall\RecaptchaFormAuthenticationListener
    parent: security.authentication.listener.abstract
    calls:
        - [setRecaptcha, [@recaptcha, %kernel.debug%]]
    abstract: true

Now you need to add recaptcha field to your form. If you are using your own form type, than add recaptcha field to your form as described [above](#Usage in forms).

Otherwise, add those lines to your Controller which renders login form and to corresponding Twig template:

<?php

// your controller ...

public function yourAction(Request $request) {
    // ...
    $recaptcha = $this->createForm($this->get('form.type.recaptcha'));

    return array(
        // ...
        'recaptcha' => $recaptcha->createView()
    );
}
<!-- template -->
<label for="recaptcha_response_field">Captcha:</label>
{{ form_widget(recaptcha) }}

This documentation is based on EWZRecaptchaBundle's docs.

Roadmap

1.0.* (TODO)

  • Add docs about manual installation and usage reCAPTCHA with ajax

1.0

  • Initial version
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity