symfony / recipes-contrib

Symfony Contrib Recipes Repositories

Home Page:https://github.com/symfony/recipes-contrib/blob/flex/main/RECIPES.md

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[google recaptcha] $secret argument not being passed to the ReCaptcha instance

o-alquimista opened this issue · comments

This is the configuration added with this recipe under the services: key. I haven't touched it.

ReCaptcha\ReCaptcha:
    arguments:
        $secret: '%env(GOOGLE_RECAPTCHA_SECRET)%'
        $requestMethod: '@ReCaptcha\RequestMethod'

On the controller, this is how I'm instantiating it:

$recaptcha = new \ReCaptcha\ReCaptcha($secret);

When I perform the action the ReCaptcha is configured to watch for, I get an ErrorException with this message:

Notice: Undefined variable: secret

Shouldn't it populate the $secret variable for me? The environment variable is set at my .env.local file:

GOOGLE_RECAPTCHA_SECRET=*******************************************************

If I instantiate it without $secret:

$recaptcha = new \ReCaptcha\ReCaptcha;

I get this:

Too few arguments to function ReCaptcha\ReCaptcha::__construct(), 0 passed in /var/www/html/portfolio/src/Controller/MainController.php on line 65 and at least 1 expected

To work around this, I'm using a parameter in services.yaml and retrieving it in the controller to pass it as $secret:

google_recaptcha_secret: '%env(GOOGLE_RECAPTCHA_SECRET)%'

Try to inject it instead of creating the instance manually:

public function indexAction(\ReCaptcha\ReCaptcha $recaptcha)
{
    // use $recaptcha here
}

You're right, I forgot about that. Now it works. Thank you.