Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅在模块中加载Yii引导_Yii_Yii Extensions - Fatal编程技术网

仅在模块中加载Yii引导

仅在模块中加载Yii引导,yii,yii-extensions,Yii,Yii Extensions,我试图只在管理模块中加载Yii引导扩展,但它不起作用。我假设我需要预加载它或以某种方式启动它。。。谢谢 class AdminModule extends CWebModule { public function init() { // import the module-level models and components $this->setImport(array(

我试图只在管理模块中加载Yii引导扩展,但它不起作用。我假设我需要预加载它或以某种方式启动它。。。谢谢

    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种不同的方法可以做到这一点:

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

  • 将其预加载到
    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
    }
    
  • 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');
    }
    
    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();
    }
    
  • 以另一种方式在
    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');
    }
    
    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();
    }
    
  • 真正有效的代码: 在模块初始化函数中,只需粘贴以下代码:

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

    在主配置(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. 
    
    要将此模块组件定义为主组件,必须在模块类的init()中设置组件:

    // Set main component `bootstrap`  instead of CController->getModule()->bootstrap
    app()->setComponent('bootstrap', $this->bootstrap);
    
    在初始化中预加载它:

    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" />
    
    
    
    它以什么方式不工作?它没有加载css等…谢谢!这正是我需要的!我仍然得到错误属性“CWebApplication.bootstrap”未定义。当使用其中一个示例时:(是因为我将引导文件存储在模块扩展名中吗?@dzona,即使在模块扩展名目录中有引导,您也可以这样做(我刚刚尝试过)。您必须提供有关代码的更多详细信息(以及生成该错误的行),以及目录的配置,以及引导程序的版本,以便我能够准确地指出问题所在。否则,请查看您是否正确访问了扩展程序(使用了正确的别名-
    模块\u name.extensions.bootstrap.components.bootstrap
    )这是我的模块初始化函数init(),如果(!Yii::app()->hasComponent('bootstrap'){Yii::setpathfalias('bootstrap',dirname(FILE)。'/extensions/bootstrap');Yii::createComponent('bootstrap.components.bootstrap')->init();Yii::app()->bootstrap->register();}当我尝试用Yii::app()->getComponent(“bootstrap”)预加载它时,如果我没有在main中创建bootstrap组件,我会得到那个错误(属性“CWebApplication.bootstrap”未定义)config@dzona,您说您使用了我的示例,但您的代码完全不同:)。尝试使用我的代码,它肯定会为你工作。