L
L
Lander2017-09-22 15:45:58
CodeIgniter
Lander, 2017-09-22 15:45:58

How to add another directory to autoloader in CodeIgniter?

Good afternoon.
I am refactoring a project on codeigniter. I want to enter traits and load them automatically accordingly.
How to add a traits directory and explain to the framework so that it can also load files from there as well as with libraries and with models...?
I really don't want to edit Core\Loader.php.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2017-09-22
@usdglander

In short, I got out of the situation so far in this way: I added the code to the end of the Config/autoload.php file

spl_autoload_register(function($class) {
    if (substr($class, -5) == 'trait') {
        require_once (__DIR__ . '/../traits/' . $class . '.php');
    }
});

Since autoloaders stack, sooner or later this one will also be executed, and since there was no concept of traits in the project before, conflicts should not arise.

I
iRedds, 2017-09-22
@iRedds

Connect composer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question