How to access a component of a module from within the module itself

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

The Yii documentation states:

An application module may be considered as a self-contained sub-application that has its own controllers, models and views and can be reused in a different project as a whole. Controllers inside a module must be accessed with routes that are prefixed with the module ID.

So practically, a module can be handled just as an application, with only minor differences.

Supposing you were having a custom component 'foo' in your application:

return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'preload'=>array('log'),
'import'=>array(
	'application.models.*',
	'application.components.*'
),
'components'=>array(
	'db'=>array(
	),
	//right down here:
	'foo' => array(
		'param1' => 'val1'
	)
),

And you need to move it inside a module called 'bar':

return array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
	'preload'=>array('log'),
	'import'=>array(
		'application.models.*',
		'application.components.*'
	),
	'components'=>array(
		'db'=>array(
		)
	),
	'modules' => array(
		'bar' => array(
			'components' => array(
				'foo' => array(
					'param1' => 'val1'
				)
				
			)
		)
	)
//...

Now instead of using the component as usual:

Yii::app()->foo

You need to call it like this from within the module:

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

As you can see, your module is indeed just another application, and you can configure any parameters as you'd do with your "root" application. The only organizatory differences are:

  • a module has a module "entry point", the FooModule which extends CWebModule, whereas the root application is bootstrapped by the framework itself, starting with your entry script (index.php)
  • the application's SiteController is called inside a module DefaultController
3 1
7 followers
Viewed: 60 714 times
Version: Unknown (update)
Category: Tutorials
Tags: module
Written by: OriginalCopy
Last updated by: Roman Solomatin
Created on: Apr 7, 2009
Last updated: 11 years ago
Update Article

Revisions

View all history

Related Articles