Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading Yii Bootstrap in a module only

I'm try to load the Yii Bootstrap extension in the admin module only but it's not working. I'm assuming I need to preload it or initiate it somehow... Thanks!

    class AdminModule extends CWebModule
    {
        public function init()
        {
            // import the module-level models and components
            $this->setImport(array(
                'admin.models.*',
                'admin.components.*',
                'ext.bootstrap.components.Bootstrap',
            ));
        }

        public function beforeControllerAction($controller, $action)
        {
            if(parent::beforeControllerAction($controller, $action))
            {
                         $this->layout = 'admin';                
                         return true;
            }
            else
                return false;
        }
    }
like image 660
keeg Avatar asked Dec 12 '12 16:12

keeg


2 Answers

There are 4 different ways to do this:

  1. Add the configuration to the app's configuration (protected/config/main.php):

    'modules'=>array(
        'admin'=>array(
            'preload'=>array('bootstrap'),
            'components'=>array(
                'bootstrap'=>array(
                    'class'=>'ext.bootstrap.components.Bootstrap'
            )
        ),
    // ... other modules ...
    )    
    
  2. Preload it in the init :

    public function init()
    {
        // import the module-level models and components
        $this->setImport(array(
            'admin.models.*',
            'admin.components.*',
            // 'ext.bootstrap.components.Bootstrap', // this will go to app config for components
        ));
        Yii::app()->getComponent('bootstrap');// this does the loading
    }
    
  3. Preload in init another way:

    public function init()
    {
        // import the module-level models and components
        $this->setImport(array(
            'admin.models.*',
            'admin.components.*',
        ));
    
        $this->configure(array(
                'components'=>array(
                    'bootstrap'=>array(
                        'class'=>'ext.bootstrap.components.Bootstrap'
                    )
                )
        ));
        $this->getComponent('bootstrap');
    }
    
  4. Preload in init yet another way:

    public function init()
    {
        // import the module-level models and components
        $this->setImport(array(
            'admin.models.*',
            'admin.components.*',
        ));
    
        $this->configure(array(
                'preload'=>array('bootstrap'),
                'components'=>array(
                    'bootstrap'=>array(
                        'class'=>'ext.bootstrap.components.Bootstrap'
                    )
                )
        ));
        $this->preloadComponents();
    }
    
like image 168
bool.dev Avatar answered Jan 01 '23 10:01

bool.dev


When you configure the following in the main config (protected/config/main.php), example as:

'admin' => array(
      'preload'=>array('bootstrap'),
       'components'=>array(
          'bootstrap' => array(
               'class' => 'bootstrap.components.Bootstrap',
               'responsiveCss' => true,
           ),
       ),
 )

Then, you can using component as

$bootstrap = $this->getModule()->bootstrap; // with $this is a current controller. 

To define this module component to main component, you must to set Component in init() of Module class:

// Set main component `bootstrap`  instead of CController->getModule()->bootstrap
app()->setComponent('bootstrap', $this->bootstrap);
like image 41
dakiquang Avatar answered Jan 01 '23 10:01

dakiquang