CodeIgniter Forums
Possible to load and use helper function in controller filter? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Possible to load and use helper function in controller filter? (/showthread.php?tid=78304)



Possible to load and use helper function in controller filter? - evansharp - 12-31-2020

Hello,

Should it be possible to load and use a custom helper function from within a controller filter?

I want to shortcut creating a Google API client object, the class for which is being auto-loaded by composer. I need the client in most controllers, but in the filter too for auth.

I have "google_client.php" in app/Helpers:

PHP Code:
function init_google_client(){
    $client = new \Google_Client();
    $client->setAuthConfigROOTPATH 'xxxx.json' );

    [...] etc.

    $client->setRedirectUribase_url() );
    $client->setAccessType('offline');        // offline access
    
    
return $client;


Then in the before filter:
PHP Code:
helper('google_client');
$client init_google_client(); 

I get an undefined error on the init function from this.

Alternatively, is there a better way to pass an object variable from a filter into subsequent controllers? The client object cannot be serialized into the SESSION array.

Thanks for help!
Evan


RE: Possible to load and use helper function in controller filter? - evansharp - 12-31-2020

I implemented a shared service, which is working so far.