directus-8-extensions icon indicating copy to clipboard operation
directus-8-extensions copied to clipboard

Adding Helpers class to Directus

Open yagobski opened this issue 5 years ago • 4 comments

HI, i tried to add Helpers to running Directus instance by changing the composer configuration file. I can't make it work can you please share advice to configure helpers on existing directus instance. Thanks in advance

yagobski avatar Feb 21 '20 17:02 yagobski

Can you share what you have so far and what helper you are trying to use?

philleepflorence avatar Feb 21 '20 17:02 philleepflorence

Just in case, in the helper/functions.php, you have this snippet:

/*
	AutoLoad - Helper Files - Should Run at application.boot!
*/

$helpers = scandir(__DIR__);
	
foreach( $helpers as $file ) 
{
    if ('.' === $file || '..' === $file || __FILE__ === $file) continue;
    
    include_once($file);
}

You then want to include this file in the config/.php file.

After that, you can do:

use Directus\Custom\Helpers;

philleepflorence avatar Feb 21 '20 17:02 philleepflorence

I am trying to add CSV export feature but I can't make it work I have the error with helpers not initialized.

Can you please share how you include this file in /config/*.php file Thanks in advance

yagobski avatar Feb 21 '20 21:02 yagobski

To include all the helpers in the project configuration:

/*
	AutoLoad - Helper Files - Should Run at application.boot!
*/

$helpers = scandir(realpath( dirname(__DIR__) . '/public/extensions/custom/helpers' ));
	
foreach( $helpers as $file ) 
{
    if ('.' === $file || '..' === $file || __FILE__ === $file) continue;
    
    include_once($file);
}

or you can include the helper directly in your hook or endpoint file.

philleepflorence avatar Feb 21 '20 21:02 philleepflorence