Home> PHP Framework> YII> body text

How to load static resources in yii2 framework

王林
Release: 2020-12-30 09:21:43
forward
2822 people have browsed it

How to load static resources in yii2 framework

The specific operations are as follows:

(Learning video sharing:Programming video)

1. Define in assets/AppAsset Method

 * @since 2.0 */ class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; // 全局 public $css = [ ]; // 全局 public $js = [ ]; public $depends = [ // 'yii\web\YiiAsset', // 'yii\bootstrap\BootstrapAsset', // 注释掉禁用bootstrap ]; // 这是设置所有js放置的位置 public $jsOptions = [ 'position' => \yii\web\View::POS_HEAD, ]; //定义按需加载JS方法 public static function addJs($view, $jsfile) { $view->registerJsFile( $jsfile, [ AppAsset::className(), "depends" => "app\assets\AppAsset" ] ); } //定义按需加载css方法 public static function addCss($view, $cssfile) { $view->registerCssFile( $cssfile, [ AppAsset::className(), "depends" => "app\assets\AppAsset" ] ); } }
Copy after login

2. Call

request->baseUrl."/css/site.css"); // 按需加载js AppAsset::addJs($this, Yii::$app->request->baseUrl."/js/respond.min.js"); ?> beginPage() ?>        <?= Html::encode($this->title) ?> head() ?> 
beginBody() ?> endBody() ?> endPage() ?>
Copy after login

in the view. Related recommendations:yii framework

The above is the detailed content of How to load static resources in yii2 framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!