Is it possible to register Application Modules with the loader

I'm thinking that it would be really nice to be able to register the modules with the loader and then to be able to reference this with application->registerModule. Is this possible?

Current:

$application->registerModules([
    'web' => [
        'className' => CLASSNAME,
        'path' => PATH
    ]

Desired:

$application->registerModules([
    'web' => $loader->module('web')


44.7k
edited Aug '14

Maybe the loader->getClasses function could be enhanced so that it is capable of returning something so that the following would work:

$application->registerModules([
    'web' => loader->getClasses('Webird\Web\Module')
 ]);

or

 $application->registerModules([
    'web' => loader->getModuleClass('Webird\Web\Module')
 ]);


83.4k

You just have to create a class that loads the definition:

class MyLoader
{
    public function getClasses($namespace)
    {
        return function($di) use ($namespace) {
            $di->setShared('view', function() use ($view) {
                $view->setViewsDir('../apps/frontend/views/');
                return $view;
            });
        };
    }

}
$application->registerModules([
    'web' => loader->getClasses('Webird\Web\Module')
]);


44.7k
edited Aug '14

In my project I'm using a real Module class and Module::registerAutoloaders() to add to it. I still want the Module itself to register its particular namespaces. So I was having some difficulties passing in the original loader to add more to it.

The main issue that I need this for was to be able to try to instantiate a module so that I run a file-by-file Volt template compilation for a module. I'm just trying to do it slick to keep paths DRY.

I'm thinking the CLI interface exposes some not so well thought out aspects of Phalcon.

All of this needs to be done so that POEdit can grab strings from the compiled volt templates (as PHP). Then later to create a production profile of completely precompiled volt templates. This seems optimal since xgettext natively supports PHP and so it removes a Twig xgettext parser and I need to batch compile the templates anyways.

edited Aug '14

@dschissler I had prepare AutoRoute plugin look at the solution and maybe it can help you update it to something what you want :) Phalcon AutoRoute plugin

The plugin will help u to automatic load module, controllers, actions with one global route. How it works ?

On application boot plugin get data from configuration of modules and parse all controllers and actions into plugin class, so u can access from class all modules, all controllers and all actions, when the plugin have all loaded you it will compare url by the loaded data about modules then configure router.

The plugin automaticaly check if module, controller, action exist when not it will redirect to the errorController with action error404Action.

Maybe it can help you with resolving your problem :)

Sorry for english in documentation, if you will be have some question feel free to contact me on skype: softdream2122



44.7k

Thanks and I'll take a loonger look at that later because I can't figure out what it does now.

I think that I figured it out. I simply needed to call $loader->registerNamespaces(..., true) to have it merge instead of clobbering.