Home >PHP Framework >YII >How to import image resources into yii

How to import image resources into yii

王林
王林Original
2020-03-11 11:57:493162browse

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.

How to import image resources into yii

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(&#39;test&#39;) ?>  
    $(function($) {  
      $(&#39;#mybutton&#39;).click(function()
      {  
         alert(&#39;OK&#39;);  
      });  
    });  
<?php $this->endBlock() ?>  <?php $this->registerJs($this->blocks[&#39;test&#39;], \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(&#39;test&#39;) ?>  
    $(function($) {  
      $("#mybutton2").click(function()
      {  
          $(&#39;#loading&#39;).show();
      });  
    });  
<?php $this->endBlock() ?>  <?php $this->registerJs($this->blocks[&#39;test&#39;], \yii\web\View::POS_END); ?>
<!-- 引入图片  -->
<img alt="" src="/static/images/5badcb9ebfe4c.png" class="img-class"><br>
<img alt="" src="<?php echo Url::to(&#39;@web/static/images/5badcb9ebfe4cpp.png&#39;); ?>" 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn