loading Yii Bootstrap in a module only

2019-04-08 17:05发布

问题:

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;
        }
    }

回答1:

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();
    }
    


回答2:

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);


回答3:

Really working code : in your module init function just paste the following code :

Yii::setPathOfAlias('bootstrap', Yii::getPathOfAlias('ext.bootstrap'));
Yii::app()->setComponent('bootstrap', array('class'=>'bootstrap.components.Bootstrap'));


回答4:

protected/config/main.php

'modules'=>array(
    'admin'=>array(
        'preload'=>array('bootstrap'),
        'components'=>array(
            'bootstrap'=>array(
                'class'=>'ext.bootstrap.components.Bootstrap'
        )
    ),

)    

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
}

If it doesn't work then paste your url into that file, which form you are using bootstrap

<link rel="stylesheet" type="text/css" href="/pojects/fcorginal/assets/b2e88ad5/bootstrap/css/bootstrap.min.css" />