CodeIgniter Forums
Loading vendor helper into global namespace - 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: Loading vendor helper into global namespace (/showthread.php?tid=79341)



Loading vendor helper into global namespace - stopz - 05-31-2021

Hey! Is there a convenient way to load a helper from vendor package into global namespace?

Currently what works (and what I could not find anywhere by googling) is to set it into App/Config/Autoload.php

PHP Code:
public $files = [
    APPPATH '../vendor/MyName/MyPackage/src/Helpers/myhelper_helper.php'
]; 

Also, I have tried to use VENDORPATH here but it's undefined by the time of Autoload.php file gets ignited.


In the docs https://codeigniter.com/user_guide/general/helpers.html it states that only way to load helper is by:

PHP Code:
helper('myhelper'); 

But I have an essential helper in my vendor/myname/pacakgename/src/Helpers/myhelper_helper.php that I would like to be accessible trough out my application at all times.
I have tried:

Setting load call into vendor/..../composer.json

PHP Code:
{
    "autoload": {
        "psr-4": {"MyName\\MyPackage\\""src/"},
        "files": [
            "src/Helpers/myhelper_helper.php"
        ]
    }


Creating a vendor/myname/mypackage/src/Config/Autoload.php and setting it up as part of $files array.
This is not getting discovered or i'm doing something very wrong.

Will anything be changed in this regard or is what I described as working solution the way to go?
Or did I misunderstood anything and there already is a very convenient and elegant solution for this?


RE: Loading vendor helper into global namespace - InsiteFX - 05-31-2021

Look in the root of the app folder just under the Views folder see the file Common.php that is for loading
things you need first.


RE: Loading vendor helper into global namespace - stopz - 06-01-2021

(05-31-2021, 08:58 PM)InsiteFX Wrote: Look in the root of the app folder just under the Views folder see the file Common.php that is for loading
things you need first.

I've looked into Common.php and I understand that I can define a function there that will be globally accessed. But the question was about loading a Vendor Helper into global namespace and if there is a neat solution for that in CodeIngiter.

At the moment there's also no way of using VENDORPATH in Common.php thus I'm back with requesting my helper almost like I did in app/Config/Autoload.php

PHP Code:
require_once  '../vendor/myname/mypackage/src/Helpers/myhelper_helper.php'

Is there a way to achieve global namespace helper declaration from within a vendor/..../package without alteration of the main application?