Welcome Guest, Not a member yet? Register   Sign In
Autoload class in codeigniter
#1

How to autoload class and its namespace in codeigniter 4 without manual adding to autoload.php config file. From the autoloader class i saw a method name addNamespace(). So where is the best time to use this method so that the config() function can recognise it and its configs files. I discovered it modules configs can't be recognise by the config() function after all the registering class and its namespace by the autoloader class. I will be happy if anyone can help me on this thanks
Reply
#2

If you want the classes in your module to be autoloaded, create a Composer package.
See https://codeigniter4.github.io/CodeIgnit...kages.html
Reply
#3

(10-14-2023, 01:17 AM)kenjis Wrote: If you want the classes in your module to be autoloaded, create a Composer package.
See https://codeigniter4.github.io/CodeIgnit...kages.html

I am not trying to use composer like I stored some of the condition in the db., so I want to load the class once the conditions are true.
Reply
#4

What do you want to do?
How do you install the class to be loaded?

You can save the namespace and the path somewhere,
and call addNamespace() before calling the class, the class will be autoloaded.


PHP Code:
        Services::autoloader()->addNamespace(
            'Acme\Blog',
            __DIR__ '/../../acme/blog/src'
        ); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB