Search code examples
cssyiimoduleintegrate

Yii: Integrating a CSS-File on the Module Level


An excerpt from my

protected/config/main.php // given situation

looks like:

'modules' => array(
    'services' => array(
        'modules' => array(
            "myModule"
        )
    )
),
....
....

and for "myModule" I want to integrate a CSS-File. I could not find information about this issue in the documentation. How could I add a CSS-File on the module-level?

Something like that would be for example awesome:

'myModule' => array(
    'css-file' => array(
        'css-file-name' => css-file-path,
    ),
),

Solution

  • one must change to the module directory and find the myModuleNameModule.php file and add the following (into the function init()):

    $file = Yii::getPathOfAlias('myModuleName').DIRECTORY_SEPARATOR."further_directory_structure";
    $url  = Yii::app()->getAssetManager()->publish($file);
    $cs   = Yii::app()->getClientScript();
    $cs->registerCssFile($url);
    

    now the css-file is reachable from all views within this module.