我创建了一个 testasset.php 如下:
<?php
namespace app\assets;
use yii\web\AssetBundle;
class TestAsset extends AssetBundle {
/*public $basePath = '@webroot';
public $baseUrl = '@web'; */
public $sourcePath = '@app/assets';
public $css = [
'/css/test.css',
];
public $js = [];
public $depends = [
/* 'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset', */
];
}
在控制器中,我只是渲染在资产上方注册的视图文件。
看法 :
<?php
use app\assets\TestAsset;
TestAsset::register($this);
$this->beginPage(); ?>
<html>
<head>
<title>Register JS and CSS</title> <?php
//Yii::$app->view->registerCssFile('/css/style.css');?>
<!-- <link rel="stylesheet" href="/css/test.css" type="text/css" /> -->
</head>
<body>
<div id="page">
<div id="logo">
<h1><a href="/" id="logoLink">Register JS and CSS Test</a></h1>
</div>
<div id="nav">
<ul>
<li><a href="#">!</a></li>
<li><a href="#">@</a></li>
<li><a href="#">#</a></li>
</ul>
</div>
<div id="content">
<h2>Test</h2>
<p> XYZ...!!! </p>
<p> ABC...!!! </p>
</div>
<div id="footer">
<p> Webpage made by <a href="/" target="_blank">XYZ</a> </p>
</div>
</div>
</body>
</html>
但是我的 test.css 文件没有被注册。我也尝试过使用 basepath 和 baseurl 以及 registerCss Yii2 函数,但没有成功。
任何人请提出我的代码中缺少的地方。