Yii Framework Forum: Methods In Modules - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Methods In Modules Rate Topic: -----

#1 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 08 May 2013 - 03:19 AM

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'),
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#2 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 617
  • Joined: 23-October 10
  • Location:Romania

Posted 08 May 2013 - 03:22 AM

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

#3 User is offline   Fabrizio Caldarelli 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 282
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 08 May 2013 - 03:24 AM

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

0

#4 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 08 May 2013 - 03:29 AM

View Posttwisted1919, on 08 May 2013 - 03:22 AM, said:

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.


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
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   Ghanshyam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 18-March 13
  • Location:Ahmedabad

Posted 08 May 2013 - 03:32 AM

Hi,
You can create method in model file.
then, you can call that method, to make object of that model.
0

#6 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 08 May 2013 - 03:34 AM

View PostFabrizio Caldarelli, on 08 May 2013 - 03:24 AM, said:

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 Fabrizio, thanks for response

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

Yii::app()->controller->mymodule::mystaticMethod()
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#7 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 08 May 2013 - 03:36 AM

View PostGhanshyam, on 08 May 2013 - 03:32 AM, said:

Hi,
You can create method in model file.
then, you can call that method, to make object of that model.


Hi Ghanshyam

I mentioned on module issues not model :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#8 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 617
  • Joined: 23-October 10
  • Location:Romania

Posted 08 May 2013 - 03:46 AM

@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 ?
0

#9 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 08 May 2013 - 04:03 AM

View Posttwisted1919, on 08 May 2013 - 03:46 AM, said:

@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
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#10 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 09 May 2013 - 02:00 PM

Someone else ?
No suggestion ?
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#11 User is offline   Ben 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 266
  • Joined: 15-March 09

Posted 09 May 2013 - 02:30 PM

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.
Don't like ads in my sig...
1

#12 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 11 May 2013 - 03:45 AM

View PostBen, on 09 May 2013 - 02:30 PM, said:

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 :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#13 User is offline   Ben 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 266
  • Joined: 15-March 09

Posted 11 May 2013 - 07:20 AM

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.
Don't like ads in my sig...
0

#14 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 11 May 2013 - 03:46 PM

View PostBen, on 11 May 2013 - 07:20 AM, said:

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 :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users