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?

Thanks

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:


Yii::app()->controller->module->id

or


Yii::app()->getModule()

Hi twisted1919 and thanks for quick response!

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


 $this->setImport(array(

            'mModule.models.*',

            'mModule.components.*',

        ));

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

Hi,

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)

Yii::app()->controller->mymodule::mystaticMethod()

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:




$this->setImport(array(

            'application.modules.mmodule.models.*',

            'application.modules.mmodule.components.*',

        ));



or




$this->setImport(array(

            'models.*',

            'components.*',

        ));



Does any of the above works for you ?

well,

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

‘application.modules.mModule.components.*’,

‘application.modules.mModule.models.*’,

…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:




Yii::app()->getModule('mymodule')->method();

Yii::app()->getModule('mymodule')->staticMethod();



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:




Yii::app()->getModule('mymodule')->getComponentA();

Yii::app()->getModule('mymodule')->getModelB();

Yii::app()->getModule('mymodule')->getWidgetC();



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.*',

	));

}



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


Yii::app()->getModule('mymodule')->staticMethod();

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.

In any case thanks for your repsonse :)

You’re welcome. :)

Little background about why I suggest




// 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.

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