Home > Article > PHP Framework > How to import image resources into yii
1. Add a static folder under the web folder, which stores static resource files such as js, css, images, etc.
2. Add the following code to the AppAsset.php file
//定义按需加载JS方法,注意加载顺序在最后 public static function addJs($view, $jsfile) { $view->registerJsFile($jsfile,[AppAsset::className(), "depends" => 'backend\assets\AppAsset']); } //定义按需加载css方法,注意加载顺序在最后 public static function addCss($view, $cssfile) { $view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'backend\assets\AppAsset']); }
(Related tutorials recommended: yii framework)
3. Introduce js, css, images etc
<?php use yii\helpers\Url; /** 引入js、css文件 */ use backend\assets\AppAsset; AppAsset::register($this); AppAsset::addJs($this,Yii::$app->request->baseUrl."/static/js/jquery.min.js"); AppAsset::addJs($this,Yii::$app->request->baseUrl."/static/js/jquery.js"); AppAsset::addCss($this,Yii::$app->request->baseUrl."/static/css/index.css"); ?> <html> <head> <title>测试引入静态资源</title> </head> <body> <!-- 测试js和css --> <div id="mybutton" class="index-test">点我弹出OK</div> <?php $this->beginBlock('test') ?> $(function($) { $('#mybutton').click(function() { alert('OK'); }); }); <?php $this->endBlock() ?> <?php $this->registerJs($this->blocks['test'], \yii\web\View::POS_END); ?> <div id="mybutton2" class="index-test">点我弹出loading</div> <div id="loading" style="display: none;"> <img alt="" src="/static/images/loading.gif" > <span style="max-width:90%">数据加载中....</span> </div> <?php $this->beginBlock('test') ?> $(function($) { $("#mybutton2").click(function() { $('#loading').show(); }); }); <?php $this->endBlock() ?> <?php $this->registerJs($this->blocks['test'], \yii\web\View::POS_END); ?> <!-- 引入图片 --> <img alt="" src="/static/images/5badcb9ebfe4c.png" class="img-class"><br> <img alt="" src="<?php echo Url::to('@web/static/images/5badcb9ebfe4cpp.png'); ?>" class="img-class"><br> </body> </html>
The above is the detailed content of How to import image resources into yii. For more information, please follow other related articles on the PHP Chinese website!