Yii中css和js文件的引入有很多种方式,下面讲三种:

1.自定义路径

创建一个php文件,比如constant.php,把他放到protected/config目录下,作为一个配置文件引入

constant.php里代码如下:

define('BaseUrl', Yii::app()->baseUrl);
define('CSS_PATH', BaseUrl.'/assets/lib/css');//前台css文件路径
define('JS_PATH', BaseUrl.'/assets/lib/js');//前台js文件路径
define('IMG_PATH', BaseUrl.'/assets/lib/img');//前台p_w_picpath路径
define('CSS_ROUTE', BaseUrl.'/assets/admin/css');//后台css文件路径
define('JS_ROUTE', BaseUrl.'/assets/admin/js');//后台js文件路径
define('IMG_ROUTE',BaseUrl.'/assets/admin/img');//后台p_w_picpath路径

然后在入口文件index.php里加上:

// change the following paths if necessary
$yii=dirname(__FILE__).'/../admin/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
$constant=dirname(__FILE__).'/protected/config/constant.php';//引入自定义的路径文件
require_once($yii);
require_once($constant);
Yii::createWebApplication($config)->run();

使用的话在views/layouts下的布局文件里使用,比如coulum3.php

<link rel="stylesheet" type="text/css" href="<?php echo CSS_PATH; ?>/main.css" />
    <link rel="stylesheet" type="text/css" href="<?php echo CSS_PATH; ?>/form.css" />

2.Yii中的引入方式

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css" media="screen, projection" />
    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
    <!--[if lt IE 8]>
    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/form.css" />

3.可以使用theme来引入

第一步:main.php中配置:

'theme'=>'capital',

第二步:在根目录下的themes下创建capital文件,在capital文件下再创建css、js、img文件

第三步:视图中引入:

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl;?>/lib/datepicker/css/datepicker.css">
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl;?>/lib/jquery-ui/css/ui-lightness/jquery-ui-1.10.3.custom.css">
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl;?>/stylesheets/theme.css">