3

I've urlmanager turned on, and probably because of this fact I cannot register css and js fiels properly.

when I add $this->registerCssFile('views/A_view/A.css'); my file is attached <link href="D:\XAMPP\htdocs\somedomain.com/views/A_view/A.css" rel="stylesheet"> which seems correct. But the file is not really working on page. Further more if I'm trying to open it from debug of chrome in a new window browser goes to

http://somedomain.com/A_view/D:/XAMPP/htdocs/somedomain.com/views/A_view/A.css

Probably I have problems in url managment ? In urlManager settings I've got such rules like:

A_view/<username:\w+> => A_view/view
A_view => A_view/index
A_view/custom => A_view/custom
views/A_view => views/A_view

Shall I add something ?

David
  • 4,332
  • 13
  • 54
  • 93
  • 1
    **This has nothing to do with url manager**, you should simply publish your assets before registering it, you could create an asset bundle for this. – soju Dec 30 '14 at 17:16

2 Answers2

3

As soju commented, it doesn't really have much to do with the urlmanager.

It would be better to register your css and js files in an asset bundle, but you could also do:

$this->registerCssFile(Yii::$app->request->baseUrl.'/views/A_view/A.css'); 
deacs
  • 4,259
  • 2
  • 27
  • 37
  • Problem is that I want to add custom js and css files for some views, and for some views no. – David Dec 31 '14 at 07:10
  • also this $this->registerCssFile(Yii::$app->request->baseUrl.'/views/A_view/A.css'); makes correct url but css is not opening in this way. only if I open it with absolute path it opens d:/work/xampp/htdocs........ – David Dec 31 '14 at 07:12
  • so can I use asset bundle to add css and js files to some views that I like ? – David Dec 31 '14 at 07:15
  • where are your css and js files? if they are in the Yii view folders, then it will indeed not work. – deacs Dec 31 '14 at 08:22
  • Well then I've put them in web/assets/ but they don't work anyway. – David Dec 31 '14 at 09:14
  • you dont need to put anything in the assets folder, yii does that for you. put css files in the `css` folder in your `web` folder and change the path in the `registerCssFile` method. – deacs Dec 31 '14 at 09:27
  • Ok. puting A.css inside /web/css file. Registering $this->registerCssFile(Yii::$app->request->baseUrl.'/web/css/A.css'); But problem stays the same :( – David Dec 31 '14 at 09:33
  • When I'm debuggin page I get this in head That's the place where the file is, but it still not working. And if I'm trying to open it in a new window from debug menu I receive yii error page Not Found (#404) Unable to resolve the request "web/css/A.css". – David Dec 31 '14 at 09:36
  • Remember that your bootstrap file `index.php` in is in the web folder. `baseUrl` returns the folder that the bootstrap file is in, so try `$this->registerCssFile(Yii::$app->request->baseUrl.'/css/A.css');` – deacs Dec 31 '14 at 09:51
2

To complement the other answer, these days you can also use @web:

$this->registerCssFile('@web/views/A_view/A.css');

It makes the code a little bit cleaner.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102