Correct way to access components in your modules?

Hello,

I’m creating a module and I’ve written a web application component class that I access from the controllers in my module by calling $this->component, which I set in the controller’s init()-function.

Now I’m wondering what would be the correct way to initialize and access the module component through the module.

The only solution I came up with was to create a public variable in the module for the component and initialize it using new and by calling it’s own init()-function in the modules init()-function as shown below:




class ExampleModule extends CWebModule

{

	public $component;


	...


	public function init()

	{

		...


		$this->component = new Component();

		$this->component->init();

	}

	

	...

}



NOTE: The class- and variable names as just examples.

When using this solution I can access the module component by calling:




Yii::app()->getModule('example')->component



There must be a better way to do this that I haven’t thought of…

Suggestions anyone?

Doesn’t anyone know the answer or was my problem hard to understand? :(

I’m still struggling with this so any help would be much appreciated.

Thanks in advance.

Yes, you can do it in a more easy way:




class MyModule extends CWebModule

{

    public function init()

    {

        $this->setImport(array(

            'my.models.*',

            'my.components.*', // FooComponent.php is located there.

        ));


        $this->setComponent('foo', new FooComponent);

    }

}



Then access it in a controller:




$component = $this->module->foo;

$a = $component->a;

$b = $component->getB();



One more note:

you can also configure modules’ components in the main config file:




'modules'=>array(

    'my'=>array(

        'components'=>array(

            'foo'=>array(

                'class'=>'FooComponent',

                'a'=>'hello',

            ),

        ),

    ),

),



Thanks a lot andy_s, this solved my problem.

One thing I noticed was that if you configure parameters for your component they won’t be accessible in the components init()-function. But this is probably as it was intended.

They are only not accessible in the preinit() function.

You can access your component after you imported it:




$this->setImport(array(

    'my.models.*',

    'my.components.*',

));


echo $this->foo->bar; // Prints a value you set in a config file.



I’ve been struggling with this the last hour…Thanks!

To access any module component from any place in application just add that magic call to main module file


public function __call($name, $args)

{

    $reflect  = new ReflectionClass($name);

    $instance = $reflect->newInstanceArgs($args);

    return $instance;

}

So now you can call


Yii::app()->getModule('modulename')->Classname();

that is equvalent to new Classname();

Hi

I have something related but can get my way through maybe someone has a solution

for loading messages from my module

in mySample


echo Yii::t("organisation","test",null,$this->module->id);

it works fine

when I configure the messages in config/main.php


'components'=>array(

    'mySample' => array(

                    'class'=>'CPhpMessageSource',

                    'basePath'=>realpath(__DIR__ . '/../../../../modules/mySample/messages')



you’ll notice my modules sit outside of the usual modules folder


'modulePath' => realpath(__DIR__ . '/../../../../modules/'),

but now I would like to keep this config in mySampleCWebModule init()


$this->components =  array(

                    'class'=>'CPhpMessageSource',

                    'basePath'=>realpath(__DIR__ . '/../../../../modules/mySample/messages')

                );

but then the translate doesn’t find the messages

if anyone sees the problem it would make my day

I did it like this because


echo Yii::t($this->module->id.".organisation","test")."</br>";

doesn’t work keeps saying include(‘mySample.php’) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

error that I don’t quite understand actually ?

Thanks