loading Yii Bootstrap in a module only

人盡茶涼 提交于 2019-12-06 02:57:40

问题


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" />


来源:https://stackoverflow.com/questions/13844509/loading-yii-bootstrap-in-a-module-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!