我如何在 yii 模块中添加脚本和样式表 [英] How do i add scripts and stylesheets inside yii module

查看:23
本文介绍了我如何在 yii 模块中添加脚本和样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 yii 的新手.我刚刚在 yii 中创建了一个模块文件结构如下

 -yii-受保护-模块-行政-控制器-模型-看法-布局-main.php-css-style.css-图片-logo.jpg

我能够将布局设置为这样

'modules'=>array(//取消注释以下内容以启用 Gii 工具'管理员'=>数组('layoutPath' =>'受保护的/模块/管理/视图/布局', ;

)

现在布局是从管理模块呈现的,问题是我无法使用

加载样式表

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/protected/modules/admin/css/reset.css" media="all">

有谁知道在yii中加载样式表的正确方法

解决方案

受保护文件夹下的所有内容确实是受保护且不可公开访问的.

在您的情况下,您正在使用模块并且您的文件位于受保护的文件夹中,您需要发布"它们以供公众访问.Yii 中已发布内容的默认公共文件夹称为资产".并发布我们将使用 CAssetManager.

首先创建一个文件夹,其中包含您需要公开访问的所有 css、js 和图像.随意命名它,但标准是它的资产",所以你的文件结构看起来像这样:

 -yii-受保护-模块-行政-控制器-模型-看法-布局-main.php-资产-css-style.css-js-图片-logo.jpg

在您的模块中创建一个属性,该属性将存储已发布资产的公共 url,以及访问它的方法.

私有 $_assetsUrl;公共函数 getAssetsUrl(){if ($this->_assetsUrl === null)$this->_assetsUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('admin.assets') );返回 $this->_assetsUrl;}

然后您可以像这样访问您的资产:

module->assetsUrl.'/images/logo.png'),数组('/xxii'));?>

进一步阅读

i am new to yii. I just created a module in yii the file structure is as follows

 -yii
  -protected
     -modules
        -admin
           -controller
           -model
           -view
               -layout
                    -main.php
           -css
               -style.css
           -images
               -logo.jpg

i was able to set the layout to like this

'modules'=>array(
    // uncomment the following to enable the Gii tool

           'admin'=>array(
                 'layoutPath' => 'protected/modules/admin/views/layouts',  ;

)

and now the layout is rendered from the admin module the problem is that i cant load the stylesheets using

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/protected/modules/admin/css/reset.css"  media="all">

Does anybody knows the correct way to load stylesheets in yii

解决方案

Everything under your protected folder its indeed, protected, and not public accessible.

in your case, that you are using a module and your files are inside protected folders, your need to 'publish' them to be public accessible. the default public folder for published stuff in Yii its called 'assets'. and to publish we will be using CAssetManager.

First create a folder that contains all your css, js and images that you'd need public access to. name it whatever you want, but the standard its 'assets', so taht your file structure looks like this:

 -yii
  -protected
     -modules
        -admin
           -controller
           -model
           -view
               -layout
                    -main.php
           -assets
               -css
                   -style.css
               -js
               -images
                   -logo.jpg

In your module create a property that will store the public url of the published asset, and a method to access it.

private $_assetsUrl;

public function getAssetsUrl()
{
    if ($this->_assetsUrl === null)
        $this->_assetsUrl = Yii::app()->getAssetManager()->publish(
            Yii::getPathOfAlias('admin.assets') );
    return $this->_assetsUrl;
}

You can then access your assets like this:

<link rel="stylesheet"
         type="text/css"
         href="<?php echo $this->module->assetsUrl; ?>/css/main.css"/>
   ...
   <div id="logo">
   <?php echo CHtml::link(
                 CHtml::image($this->module->assetsUrl.'/images/logo.png'),
                 array('/xxii')); ?>
   </div>

Further reading

这篇关于我如何在 yii 模块中添加脚本和样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆