Using Trait in Slim3

Hello, I’m using Slim 3.x with several forms. Now I need to protect them using recaptcha v3, so I assumed that a trait should be the right way to DRY

I have a Helpers folder under app
/–app/
|–Controllers
|–Helpers
|–Middleware
|–Models
|–Validation

So I’ve created a trait under Helpers
<?php
namespace App\Helpers;

trait Captcha
{

    public function validateCaptcha($token, $action)
    {

    // Build POST request:
        $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
        $recaptcha_secret = $this->container->get('settings')['captcha']['secret'];
        $recaptcha_response = $recaptcha;

    // Make and decode POST request:
        $recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
        $recaptcha = json_decode($recaptcha);

    // Take action based on the score returned:
        if ($recaptcha->score >= 0.5 && $recaptcha->action === $action) {
            return true;
        } 
        return false;
    }
}

Then, in my controller I have

use App\Helpers\Captcha;

class ProccessFormController extends Controller
{
use Captcha;

public function store($request, $response, $args)
{
    if (! validateCaptcha($getParam->recaptcha_response, 'action1')) {
         $this->flash->addMessage('error', 'Captcha error');
        return $response->withRedirect($this->router->pathFor('home'));
    }
......
}

When submitting a form, controller fires an error

**Type:** Error
**Message:** Call to undefined function App\Controllers\validateCaptcha()
**File:** /home/dummy/app/Controllers/ProccessFormController.php
**Line:** 22

If I put validateCaptcha() function inside my controller, everything works fine, so the point is that my controller is unable to load and use the trait.

Thanks for any idea on how to solve it.

Hi @faridsilva !
I think you can try to call the function of your class using: $this->validateCaptcha.

Yes, you really have the point. I miss $this reference, but sadly it is not solving the error
Thankyou @maurobonfietti for your reply anyway.

If I put function validateCaptcha() into my controller everything works fine invoking $this->validateCaptcha()
Captcha is solved by google and result is accurate, but if I leave that function in the trait it doesn’t work.

Oh, that’s strange, and you get the same error message:
Call to undefined function ...
??

I saw a few times Traits in Slim 3 and it works fine :smiley:.

This little example works for me:

<?php

namespace App\Helpers;

trait MyTrait
{
    public function printMyTrait()
    {
        return 'Hello Trait!';
    }
}
<?php

namespace App\Controller;

use App\Helpers\MyTrait;

class ProccessMyTrait
{
    use MyTrait;

    public function getTrait()
    {
        echo $this->printMyTrait();
    }
}
$app->get('/trait', 'App\Controller\ProccessMyTrait:getTrait');

(using Slim 3 and PHP 7.4)

Yes, I’m utilizing traits exactly in that way under Laravel, and in other plain php apps. According to PHP docs that’s the way to use them, so it taken me aback, but I will dig deeper in debug this point and will come back with any news.
Thank you again for your kindly attention.

I’m ashamed because I forgot that the site in which I’m trying to use traits is behind a cloudflare cdn.
So when I realised it and clear all caches turning on development mode, my trait becomes usable.
So we can trust that that deployment is fully accurate.

@maurobonfietti Thank you again for your patience a attitude!

Regards.