1

I am new to PHP and trying to call reCaptcha API in CraftCMS. I have the following variable:

$captcha = Craft::$app->getRequest()->getParam('g-recaptcha-response');

But I get the following error when I submit a form:

Fatal error: Uncaught Error: Class 'Craft' not found

How can I import the Craft class?

1

This is not really specific to Craft, but you should import Craft at the beginning of your class file like this:

<?php

use Craft;

...

I suggest you take a look at that documentation for more details about how to import PHP classes: https://www.php.net/manual/en/language.namespaces.importing.php

  • use Craft; gave the following error: Warning: The use statement with non-compound name 'Craft' has no effect – Amir Jul 9 at 18:11
  • In what context are you using the Craft object? Are you using a Module ou a Plugin? If not, you should include the Framework autoloader from the vendors folder in order to use its classes. You will find an example of how to do that by looking to the index.php file in the web folder of your project. However, in your use case, using a Module looks like the way to go. – Alban Jubert Jul 11 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.