Good day. I am developing a small application on Yii 2 (using the basic template). Faced with the fact that the css-file that I want to use for the design of the module and which is located in the structure of the module is not connected. Here is the order of my actions:

  1. Created a template file for the / modules/ admin/views/layouts/main.php module
  2. In the file / modules/admin/Module.php in the init () method indicated that I would use this template for the module.
  3. In the module folder, created the css folder and in it the /modules/admin/css/admin_style.css file
  4. In the module folder I created the assets folder and in it the file /modules/admin/assets/AdminAsset.php , in which I indicated that I want to connect /modules/admin/css/admin_style.css
  5. In the template file I indicated that I will use AdminAsset:

    use app \ modules \ admin \ assets \ AdminAsset;

    AdminAsset :: register ($ this);

  6. In the developer’s panel I see that my css-file is registered, but cannot connect (status 404):

enter image description here

I suspect that the reason is that the folder with css-files is not public and you need to allow access to it .htaccess . In my application I use 2 .htaccess files : in the / web folder and in the root. In the / web folder this:

RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php 

And at the root - here is this:

 Options +FollowSymLinks IndexIgnore */* RewriteEngine on RewriteCond %{REQUEST_URI} !^/(web) RewriteRule ^assets/(.*)$ /web/assets/$1 [L] RewriteRule ^css/(.*)$ web/css/$1 [L] RewriteRule ^js/(.*)$ web/js/$1 [L] RewriteRule ^images/(.*)$ web/images/$1 [L] RewriteRule (.*) /web/$1 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /web/index.php 

Please help me figure it out, otherwise I’m not strong in the .htaccess directives.

PS There is also a desire to upload files using the / uploads folder, not / web / uploads , but this, in my opinion, is a question from the same plane

  • and admin_style.css at what address is trying to load? from site.com/assets/xxxx/admin_style.css ? - pa3py6aka
  • Please AdminAsset.php code, I think there is a mistake - pa3py6aka
  • Thank you, you pushed me to the decision. Indeed, the problem was in the paths in AdminAsset.php. I wrote it this way - and it all worked: public $sourcePath = '@app/modules/admin'; public $css = [ 'css/module_admin.css', 'css/new.css', ]; But I had to manually delete the cache from the web / assets /. How to make it not created there in DEV mode? - Vlad
  • Probably here you will find a solution - stackoverflow.com/questions/24819220/yii2-assets-clear-cache - pa3py6aka

0