2

I had created a testasset.php as follows :

<?php

namespace app\assets;
use yii\web\AssetBundle;

class TestAsset extends AssetBundle {
    /*public $basePath = '@webroot';
      public $baseUrl = '@web'; */
      public $sourcePath = '@app/assets';
      public $css = [
        '/css/test.css',
      ];
      public $js = [];
      public $depends = [
      /*  'yii\web\YiiAsset',
          'yii\bootstrap\BootstrapAsset', */
      ];
}

In controller i am simply rendering view file which register above asset.

view :

<?php 

use app\assets\TestAsset;
TestAsset::register($this);
$this->beginPage(); ?>

<html>
  <head>
    <title>Register JS and CSS</title> <?php 
     //Yii::$app->view->registerCssFile('/css/style.css');?>
    <!-- <link rel="stylesheet" href="/css/test.css" type="text/css" /> -->
  </head>
  <body>
    <div id="page">
      <div id="logo">
        <h1><a href="/" id="logoLink">Register JS and CSS Test</a></h1>
      </div>
      <div id="nav">
        <ul>
          <li><a href="#">!</a></li>
          <li><a href="#">@</a></li>
          <li><a href="#">#</a></li>
        </ul>
      </div>
      <div id="content">
        <h2>Test</h2>
        <p> XYZ...!!! </p>
        <p> ABC...!!! </p>
      </div>
      <div id="footer"> 
        <p> Webpage made by <a href="/" target="_blank">XYZ</a> </p>
      </div>
    </div>
  </body>
</html>

But my test.css file is not getting registered. I had tried using basepath and baseurl as well and registerCss Yii2 function but with no success.

Anyone Please suggest where i am lacking in my code.

3
  • view file generally does not have the <html>, <head>, <body> tags as they are rendered through the layout file. Are you not using layouts? Commented Dec 28, 2016 at 11:14
  • I am using layout file in my main project. its just a test code that i have shared so does not have any separate layout file. Commented Dec 28, 2016 at 11:17
  • If you are not using layouts then you can try to render the header asset files explicitly as suggested in @Bizley's answer below. Commented Dec 28, 2016 at 11:21

3 Answers 3

2

You need to add

<?php $this->head() ?>

preferably somewhere between <head> and </head> for the header asset files to be rendered.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry but this does not worked out, neither do adding $this->beginBody() and endBody in body tag worked.
Could you add rendered source code for this page? From the beginning to the </head> tag.
<html> <head> <![CDATA[YII-BLOCK-HEAD]]> <title>Register JS and CSS</title> <!-- <link rel="stylesheet" href="/css/test.css" type="text/css" /> --> </head>
@user3350093 did you call <?php $this->endPage() ?> at the bottom of your view? Because the replacement of the placeholder <![CDATA[YII-BLOCK-HEAD]]> happens after the page rendering is completed.
No i was not using endPage(). Adding this to the bottom of view solved my problem.
1

Simply add this in view file

<?php 
   $this->registerCssFile('PATH_TO_FILE');
?>

1 Comment

Thanks.. adding endpage at the bottom of my view works.
0

I have had the same issue on one of my advanced application template projects. I've compared my layout/main.php file with the same on the Yii2 advanced application template and found missing $this->endPage() at the end of file. It was removed by mistake and without it no js, ccs assets worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.