5

我尝试仅在管理模块中加载 Yii Bootstrap 扩展,但它不起作用。我假设我需要预加载它或以某种方式启动它......谢谢!

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

4 回答 4

10

有 4 种不同的方法可以做到这一点:

  1. 将配置添加到应用程序的配置 ( protected/config/main.php ):

    'modules'=>array(
        'admin'=>array(
            'preload'=>array('bootstrap'),
            'components'=>array(
                'bootstrap'=>array(
                    'class'=>'ext.bootstrap.components.Bootstrap'
            )
        ),
    // ... other modules ...
    )    
    
  2. 将其预加载到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. init以另一种方式预加载:

    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. init以另一种方式预加载:

    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();
    }
    
于 2012-12-12T17:52:24.860 回答
2

当您在主配置(protected/config/main.php)中配置以下内容时,例如:

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

然后,您可以将组件用作

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

要将此模块组件定义为主组件,您必须在 Module 类的 init() 中设置 Component:

// Set main component `bootstrap`  instead of CController->getModule()->bootstrap
app()->setComponent('bootstrap', $this->bootstrap);
于 2013-11-19T09:05:12.300 回答
1

真正有效的代码:在您的模块初始化函数中只需粘贴以下代码:

Yii::setPathOfAlias('bootstrap', Yii::getPathOfAlias('ext.bootstrap'));
Yii::app()->setComponent('bootstrap', array('class'=>'bootstrap.components.Bootstrap'));
于 2013-09-26T21:57:25.750 回答
0
protected/config/main.php

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

)    

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

如果它不起作用,则将您的 url 粘贴到该文件中,您正在使用引导程序的形式

<link rel="stylesheet" type="text/css" href="/pojects/fcorginal/assets/b2e88ad5/bootstrap/css/bootstrap.min.css" />
于 2014-06-06T11:25:03.330 回答