tags:

views:

1028

answers:

6

hi, In CakePHP,if i give href links as href="/css/main.css" it is not refering to the css folder in the webroot. Only when I mention href="http://localhost/cake/app/webroot/css/main.css" the css gets applied.

<link type="text/css" rel="Stylesheet" href="/css/main.css" media="screen,projection" />

This does not apply the specied css.

What is the reason for this? Why is the code not identifying the correct folder?

A: 

If your absolute URL looks like

http://localhost/cake/app/webroot/css/main.css

and your URL is

/css/main.css

I suppose the browser translates the URL you gave to

http://localhost/css/main.css

(you can check that with Firebug, "net" tab for instance)

If that's the case, you should :

  • use a relative URL with no leading /
  • use an absolute URL that refers to the root URL

But, in my opinion, using the URL that starts with 'http://' is the best way to be sure that your CSS inclusion is always OK, no matter which directory or URL you are into...

Pascal MARTIN
well, if I use url starting with http://, I have a problem. When I develop it and give it to someone else, he must be able to run the application without changing the code..For I may have it in a folder called cake, but he might have a different folder name. so I thought there might be a way to apply the css with just using /css/main.css
Angeline Aarthi
this URL could be defined in a configuration file, and given to your views (if you have some kind of bootstrap code with Cake, or a Controller-class inherited by every Controller of your application, this might me the right place to do that) ; or there is probably a way with the framework you're using to determiner the base URL of the application (I don't use Cake myself, so I can't give you any hint about that ; sorry)
Pascal MARTIN
A: 
Ben
In which file should this base url be defined?
Angeline Aarthi
+3  A: 

Because it starts with a /, it is treated as an absolute path (from the root of the site). The browser translates it to

http://localhost/css/main.css

You can either specify the correct absolute path

/cake/app/webroot/css/main.css

or the full path

http://localhost/cake/app/webroot/css/main.css

or a relative path, for example

../css/main.css
immibis
yep,that worked.. thank u..
Angeline Aarthi
A: 

When you deploy cake, the url should just be /css/main.css as the server's DocumentRoot will point to the cake/app/webroot directory.

I suggest reading this article from Cake's online docs for more info. Note that this article refers to the 1.1 version of Cake, but should work in 1.2 as well.

Good luck!

mjaz
+1  A: 

Why are you not using core helper? It will generate required path to CSS file Inserting Well-Formatted elements

And check main configuration file(/app/config/core.php), maybe you are not using mod_rewrite. Check core.php for commented this line Configure::write('App.baseUrl', env('SCRIPT_NAME'));

Maxim
+2  A: 
echo $html->css('main');

BOOK

API

Alexander Morland
http://book.cakephp.org/view/206/Inserting-Well-Formatted-elements#css-831
deizel