We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

dynamic registration namespaces

Hello, i need dynamically register new library in controller. I would like to include a new library with a one new namespace, but with multiple files. How can I do that?

I have to do in the controller, because the configuration of the library is stored in the database.

Please help.



85.5k
Accepted
answer

i n module.php or application.php

$loader = new Loader();

$loader->registerNamespaces([
'MYAPP\Plugins' => '/var/www/myapp/Plugins/
]);

$loader->register();

inside /var/www/myapp/Plugins/ you can have folders and files, but you have to keep the concept

if you have file : /var/www/myapp/Plugins/Auth/Users.php this file has to be with namespace MYAPP\Plugins\Auth and class name has to be Users

keep in mind for small - big letters, you can check the docs for namespace includes