2

I have create a admin module in the yii and I want the module use the customized layout. so I placed the css and js file in protected/module/admin/assets,how can I include the css and js file in this folder?

and now I have another problem: I have put the file in the protected/modules/admin/css/assetsbootstrap.min.css folder,and I can not include it anyway,even I use the absolute path.I also try it in the other way like:

<link rel="stylesheet" href="<?php echo Yii::app()->baseUrl;?>/protected/modules/admin/css/assetsbootstrap.min.css" />

is this way does not work in the module?

3

2 Answers 2

2

Check this extension out

Yii admin module

Open AdminModule.php, there is location where used for configuration your admin module. I just post the parts what you need

public function getAssetsUrl()
    {
        if (!isset($this->assetsUrl))
        {
            $assetsPath = Yii::getPathOfAlias('admin.assets');
            $this->assetsUrl = Yii::app()->assetManager->publish($assetsPath, false, -1, $this->forceCopyAssets);
        }

        return $this->assetsUrl;
    }


protected function registerCoreCss()
    {
        Yii::app()->clientScript->registerCssFile($this->getAssetsUrl() . '/css/admin.css');
    }

protected function registerScript(){
    $js_arr = array('jquery.min.js', 'jqueryui.js'); //put what js file name that you need to import from admin assets folder
    foreach($js_arr as $filename){
        Yii::app()->getClientScript()->->registerScriptFile($this->getAssetsUrl().'/js/'.$filename, CClientScript::POS_END);
    }
}

And then in init()

public function init(){
    //set import ...
    // configure module ...
    // configure component  ....

    $this->registerCoreCss();
    $this->registerScript();

}
Sign up to request clarification or add additional context in comments.

1 Comment

I think this way is a little complexed,is there some other way?In my opinion,i want to define a constant in the 'main' config file located in the config folder,but it does not work!
1

Here you are:

$baseUrl = Yii::app()->baseUrl; 
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl.'/js/yourscript.js');
$cs->registerCssFile($baseUrl.'/css/yourcss.css');

OR

by editing config/main.php

// application components
  'components'=>array(
         // ...
        'clientScript'=>array(
            'packages'=>array(
                'jquery'=>array(
                    'baseUrl'=>'//ajax.googleapis.com/ajax/libs/jquery/1/',
                    'js'=>array('jquery.min.js'),
                )
            ),
        ),
        // ...
  ),

See more: Yii include Javascript, css file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.