cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Add Autoload Option for Module to be loaded anytime

Open alphanyx opened this issue 10 years ago • 9 comments

Modules will only be loaded if a route leads to the Module. It would be great to load Modules without routes or controllers.

The Purpose of my request is that i want to split my application into a database module that handles all the storage and data evaluation and a frontend module that requires the database module.

The Database Module has no controllers and will never be loaded by the Phalcon Application.

I Suggest an parameter while registering the Module to define wether it should be loaded on startup (load always) or just for specific routes (current default behaviour).

Something like in this example:

$application->registerModules([
    'base' => [
        'className' => 'Application\Base\Module',
        'path' => __DIR__ . '/apps/base/Module.php',
        'autoload' => true // or 'on_startup' 
    ]
]);

Related Topics: https://forum.phalconphp.com/discussion/14857/phalcon-module-definition-understanding#C43199

alphanyx avatar Nov 22 '16 23:11 alphanyx

I like this idea.

Jurigag avatar Nov 23 '16 15:11 Jurigag

I think more about OOP approach. Something like Module Manger with events

sergeyklay avatar Nov 23 '16 18:11 sergeyklay

What do you mean? Do you have a code example?

alphanyx avatar Nov 23 '16 20:11 alphanyx

$module = new Module('base', true); // name, autoload
$module->setControllersNamespace();
// ...

$moduleMannager = new Manager();
$moduleMannager->setEventsManager($em);
// ..

$moduleMannager->add($module);
// ...

$moduleMannager->register($application);

Cc: @Jurigag, @dschissler, @virgofx

sergeyklay avatar Nov 24 '16 01:11 sergeyklay

I very much like the assumption that only a single module will be loaded during a normal execution and with this feature we would be moving away from it. I think that we should think of a module as a sub-app.

https://github.com/phalcon/cphalcon/issues/12252

As detailed in the issue above I think that Phalcon version 3 we should move to encourage this approach:

$application = new Application($di);
$application->registerModules([
    'org'   => OrgModule::class,
    'site'  => SiteModule::class,
    'admin' => AdminModule::class,
    'api'   => ApiModule::class,
]);

The loader can/should be setup before this point to be able to find the paths by the class names above. Currently the Application class does what I call "a poor man's loader" and its not a proper separation of concerns. Currently to avoid this IMO incorrect behavior in the Phalcon ecosystem I'm using the same approach as used by the Dev Tools modules project template:

(wildcard is because its from the Dev Tools template for creating a project)

    $application->registerModules([
        'frontend' => ['className' => '@@namespace@@\Modules\Frontend\Module'],
    ]);

Supporting the former API is fully compatible with version 3 without breaking semver and then I propose that version 4 drop support for the current method.

If we stick religiously to this approach then it ensures that a module is just the later phase of the routing process. So I propose that we not interfere with this current approach of treating a Module as if it were a subapp.

However, the use case that @alphanyx is bringing up is a nice feature because the current Module definition neatly encapsulates setting up the loaders as well as dependency injector. The thing that does make me pause when thinking about this is that the DI services can get clobbered from one module to another since it would be shared. Otherwise for perfect encapsulation the DI would not be shared (more than one DI) then I'd ask @alphanyx to consider what that would look like.

dschissler avatar Nov 24 '16 02:11 dschissler

In a perfect world with BC issues imagine this:

$application = new Application($di);
$application
    ->registerModules([
        'db'   => DbModule::class,
    ])
    ->registerSubapps([
        'frontend'   => FrontendSubapp::class,
    ])

So then it is not necessary to use a module since you can just use the current approach to add services to a DI but if you wanted to separate it then you break it down like this.

dschissler avatar Nov 24 '16 02:11 dschissler

Hi,

I think it would be consistent that each module can register it's own routes.

Currently the dispatcher tells which module to load according to the routes processed, so the module can't register it's own routes directly.

A way would be add a parameter with a router group instance to the register modules.

kornerita avatar Apr 11 '17 19:04 kornerita

I think that would need to be a Phalcon 4-5 change. It seems quite different than the current way.

dschissler avatar Apr 16 '17 01:04 dschissler

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

stale[bot] avatar Apr 16 '18 20:04 stale[bot]