How to load Yii modules component?

4.9k views Asked by At

When I did the steps like described here I just get with config in config/main.php:

<?php
return array(
  'modules' => array(
    'tracking' => array(
      'components' => array(
        'tracking' => array(
          'foo' => 'bar'
)))));

this result, when I var_dump Yii::app()->getModule('tracking'):

object(TrackingModule)#148 (20) {
  ["defaultController"]=>  string(7) "default"
  ["layout"]=>  NULL
  ["controllerNamespace"]=>  NULL
  ["controllerMap"]=>  array(0) {}
  ["_controllerPath":"CWebModule":private]=>  NULL
  ["_viewPath":"CWebModule":private]=>  NULL
  ["_layoutPath":"CWebModule":private]=>  NULL
  ["preload"]=>  array(0) {}
  ["behaviors"]=>  array(0) {}
  ["_id":"CModule":private]=>  string(8) "tracking"
  ["_parentModule":"CModule":private]=>  NULL
  ["_basePath":"CModule":private]=>  string(79) "..."
  ["_modulePath":"CModule":private]=>  NULL
  ["_params":"CModule":private]=>  NULL
  ["_modules":"CModule":private]=>  array(0) {}
  ["_moduleConfig":"CModule":private]=>  array(0) {}
  ["_components":"CModule":private]=>  array(0) {}
  ["_componentConfig":"CModule":private]=>  array(1) {
    ["tracking"]=> array(1) {
      ["foo"]=> string(4) "bar"
    }
  }
  ["_e":"CComponent":private]=>  NULL
  ["_m":"CComponent":private]=>  NULL
}

I expect that I can access modules component via Yii::app()->getModule('tracking')->tracking. But as you see Yii::app()->getModule('tracking') has no component, just its config.

Any suggestions what I am doing wrong?

3

There are 3 answers

2
RobM On BEST ANSWER

You have to specify class for component, for example in config (TrackingClassName):

'modules' => array(
    'tracking' => array(
        'foo' => 'bar',
        'components' => array(
            'tracking'  => array(
               'class' => 'TrackingClassName', 
            ),...
1
urmaul On

You can still access it via Yii::app()->getModule('tracking')->tracking. Component will be created before the first use.

0
Muhammad Shahzad On