Welcome Guest, Not a member yet? Register   Sign In
Loading vendor helper into global namespace
#1

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?
Reply
#2

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(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?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB