Create a components/helpers folder inside your module and import it from inside your module(setImport() in init() method). Then in your helpers folder you can have static classes that can help you inside your module.
I think from outside the module, you shouldn’t access models/ components that are shipped with the module. Think of the module as a black box. The outside world shouldn’t know about its internal structure. Otherwise, you can never refactor your module.
If it’s necessary, because the interface of the module grows to large, you can do:
From within your module, you work nearly the same as with an ordinary application. For example in one of your module’s controllers, you can do:
$myModule = $this->module;
$myModuleComponent = $this->module->componentName;
// if your module doesn't define the component, the module will look
// if its parent (normally the application) defines the component
$someApplicationComponent = $this->module->componentName;
What I always do in my modules is in its init() method:
public function init()
{
$this->setAliases(array(
'_myModuleName' => dirname(__FILE__),
));
// import the module-level models and components
$this->setImport(array(
'_myModuleName.behaviors.*',
'_myModuleName.components.*',
'_myModuleName.models.*',
));
}
as my code works too(when import in config/main.php ‘application.modules.mymodule.*’,)
mymoduleModule::staticMethod();
Obviously I could separate my module in two package (original module and extension) and use one of them in appropriate case, but as I said I want integration in one package.
// better use this
Yii::app()->getModule('mymodule')->staticMethod();
// instead of this
MyModuleModule::staticMethod();
The first version will actually create an instance of your module. While this might not necessarily be needed for every static method, many of them will probably use some components, models or other helpers that ship with the module. So by making sure you have an instance of the module created before running the methods, you also ensure that its init() method is run. And that is commonly the place where all the necessary setup for the module is placed, like for example all the import() calls.
If it is not nessesary to load entire module (especially a heavy module) then the second way is equal important. The module init (in my case) has a lot of work to do! So must be loaded only in a few cases. In summary both of ways are important according to web application needs.