Generating navigation based on enabled modules

I'm designing a system using Yii where each user loads separate modules.  Right now all that is happening is modules are getting added to the modules array in the configuration file.  I have modules embedded inside other modules up to 3 levels deep.  I was wondering if anything within the module gets called when a module is enabled, so that I can generate navigation on the fly based on which modules are enabled.  Ideally, there would be a function inside each enabled module that gets called regardless of if that module is currently being used during the current request, that way modules could register navigation items themselves.

Is there a good way to do this with Yii, or does anyone have any suggestions on the best way to accomplish this?


Not sure if this helps, but each module can have its own 'modules' array. That means, each can have a list of child modules.

BTW, are you sure 'module' is exactly what you want? Note that it may be different from what you think it is.

Yeah, I'm fairly certain a module is what I want, although I am new to Yii, so I could easily be mistaken.  Let me explain the application.

Inside the index file, instead of pulling the configuration from a static config file, it connects to a database and grabs information on the site based on the domain that was used to access the site.  It pulls everything from site name, to the dsn so it knows which database to connect to.  It also pulls from a database table what modules should be enabled.  It then generates the config file to be loaded into the application.

The whole system is a cms of sorts, that hosts several domains using the same codebase, with just different configuration and themes per domain.  Different domains have different modules enabled.  For example, one domain might need a Blog module, and another might need the Gallery module, and even another might need both.

Currently I have an Admin module that contains the admin section of the sites, with things like admin management, and a general admin area.  This Admin module contains other modules, like Blog, Gallery, etc for managing different areas of the site.  The Blog module should hopefully be able to tell the admin module about some navigation that it has, like a navigation link to add a new blog post, or to manage comments.  I was hoping to be able to have the Blog module itself define what navigation items it has, in something similar to the init() function, but since when you are in the Gallery module inside the Admin module the Blog module never gets loaded, this presents a problem.

If I really have to, I guess In the Admin layout template, I can go through and see if a module is enabled, and show it's navigation.  But I was hoping for something a little more dynamic than that, so that each Module can be 100% self contained, and affect the Admin module around it, or even other modules inside the Admin module.  For example, it would be cool if the Blog module could tell a Dashboard module that there are new posts.  But the Dashboard module would hopefully be able to accept messages from any module, and should have no knowledge of the modules, just information that is passed to it.

I hope that makes sense.


On a related note, i can do Yii::app()->getModule('admin') and get a reference to the admin module, but how can i get a reference to modules inside a module?

Yii::app()->getModule('admin/dashboard/stats') returns null.

Use $module->getModule($name) to get the named child module.

So I ended up allowing modules to register navigation items inside their init function, and loading all modules under the admin module on every page load.  With apc it takes less than a thousandth of a second to load several sub modules, and only increases ram usage by about 100k, so this is perfectly acceptable.

If anyone is interested, I used the following class to extend CWebModule:

and then inside the admin module's init():