在Yii2中我尝试添加动态css加载。所以我在web/css/custom.php中添加了php文件。现在我不能在custom.php中访问模型或会话。显示错误
现在我添加会话概念,但显示此错误Fatal error: Class 'Yii' not found
.layout文件我提到自定义文件链接。<link type="text/css" rel="stylesheet" href="<?php echo Yii::$app->request->baseUrl?>/web/css/custom.php">
$sessionArray = Yii::$app->session['settings'];
请任何人都可以帮忙。如何解决这个问题
我建议采用另一种方法:
- 创建原始布局文件
- 创建自定义动作
- 将代码放入视图文件
/视图/布局/raw.php
<?php $this->beginPage() ?>
<?php $this->beginBody() ?>
<?= $content ?>
<?php $this->endBody() ?>
<?php $this->endPage() ?>
/前端/控制器/SiteController.php
<?php
public function actionCustomCss()
{
$this->layout = "raw";
Yii::$app->response->format = 'yii'web'Response::FORMAT_RAW;
Yii::$app->response->headers->add('Content-Type', 'text/css');
return $this->render('css');
}
?>
/前端/视图/网站/css.php
html, body {
font-family: <?= implode(['Consolas', 'Arial'], ', ') ?> !important;
color: <?= "red "?> !important;
}
并使用:
<link rel="stylesheet" type="text/css" href="<?= 'yii'helpers'Url::to(['site/custom-css']) ?>">
现在,在你的视图文件中你可以使用Yii::$app等等