2016-11-03 1 views
0

Yii2では動的CSSロードを追加してみます。web/css/custom.phpにPHPファイルを追加してみましょう。モデルやセッションにアクセスできないin custom.php。何らかのエラーを示しています。Yii2 web/css/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'];

この問題を解決するには、誰でも助けてください。

+0

custom.phpファイルにYiiパッケージをインポート –

答えて

2

私は別のアプローチを示唆している:

  1. 生のレイアウトファイルを作成
  2. カスタムアクションを作成するビューファイルに

/ビュー/レイアウト/あなたのコードを入れてraw.php

<?php $this->beginPage() ?> 
<?php $this->beginBody() ?> 
<?= $content ?> 
<?php $this->endBody() ?> 
<?php $this->endPage() ?> 

/frontエンド/コントローラ/ 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'); 
    } 
?> 
/frontend/views/site/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などを使用することができます。

+0

あなたのお返事ありがとうございます。frank.itsはうまく動作しています。 –

+0

うれしいです。投票する! –

関連する問題