0

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?

Jurik
  • 3,244
  • 1
  • 31
  • 52

3 Answers3

3

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

'modules' => array(
    'tracking' => array(
        'foo' => 'bar',
        'components' => array(
            'tracking'  => array(
               'class' => 'TrackingClassName', 
            ),...
RobM
  • 432
  • 1
  • 4
  • 17
  • That worked for me. But why do I've to declare the components class name? Because when I generated the module via GII there's a line in module's init function: `$this->setImport(array( 'tracking.models.*', 'tracking.components.*', ));`. – Jurik Dec 11 '13 at 10:04
  • setImport is like PHP include_once but all files eg. in tracking/components/*.php. Setting class name in config means 'new Class' when you first use it. You have to do both. Its logical because how Yii could know which class from components? Class name can be set inside module however IMO its better only when class name is dynamically changing. – RobM Dec 11 '13 at 10:34
0

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

urmaul
  • 7,180
  • 1
  • 18
  • 13
  • When I do this (`Yii::app()->getModule('tracking')->tracking`) call I get just the php `call on a non-object` error. – Jurik Dec 10 '13 at 15:39
0

See here

Another Yii forum Post

Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130