Methods In Modules

Hi to Everyone

which is the best place to create static (or not)v methods in a custom module ?

How call this methods inside on module or from other controllers/modules?


Note: I have already set the module in config/main.php

‘mymodule’=>array(‘class’ => ‘application.modules.mModel’),

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.

What kind of static method you’ll put in your module?

To call an instance of Module you have:




Hi twisted1919 and thanks for quick response!

I have already import all models and components (without helper)





But I don’t know how call methods

for example: I have components inside module components/mycomponent.php

In template if I call mycomponent::myStaticMethod(); error occurs

include(mycomponent.php): failed to open stream: No such file or directory


You can create method in model file.

then, you can call that method, to make object of that model.

Hi Fabrizio, thanks for response

it is not working especially for call static methods (php error)


Hi Ghanshyam

I mentioned on module issues not model :)

@KonApaz - you try to call the component::staticMethod() when you run a url that invokes a controller from your module okay?

If so, you are doing it right, but for some reason, your components are not imported.

What happens if you:










Does any of the above works for you ?


The only way I found is to set in config/main.php in import region



…that works

But in modules region

‘mymodule’=>array(‘class’ => ‘application.modules.mModule’),

even if I set ‘preload’ => array(‘mymodule’…

not works!

I want like that: mymodule->myComponent::staticMethod() works inside myModule class (extends CWebModule) or in another module or controller.

In summary I want my module works like both library and module

This Is the first time that I make a module

Someone else ?

No suggestion ?

For access from outside your module, try:



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()



		'_myModuleName' => dirname(__FILE__),


	// import the module-level models and components







Hope that helps.

Hi Ben and thanks (with a vote) for your time

Yes I agree with you, the module should be a seperated mini application

But In my case I want to have an integrated package (library or extenstion and module in one).

For example I want in my template (common for all users) call an static function of the module.

your code works


as my code works too(when import in config/main.php ‘application.modules.mymodule.*’,)


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.

In any case thanks for your repsonse :)

You’re welcome. :)

Little background about why I suggest

// better use this


// instead of this


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.

Dear Ben

I agree with you except in a few cases.

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.

Thank you for sharing your experience :)