1

After setting up a fresh copy of CakePHP project, the css reference from the code is:

/my_app_name/css/cake.css 

While the real location of my css files is:

/my_app_name/webroot/css/cake.css 

Why is that?

2 Answers 2

1

Why is that?

CakePHP, as with most frameworks, makes use of url rewriting by default (not a requirement, it can also be turned off). For apache users this is implemented via .htaccess files, such as this one in the root of your application:

<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L] </IfModule> 

And another in the webroot - the presence of these files mean url rewriting just-works for the majority of users.

The rules in the application root folder rewrite all requests to the webroot folder, thus these urls are actually equivalent:

http://localhost/my_app_name/some/file.css http://localhost/my_app_name/webroot/some/file.css 

The primary purpose of these rewrite rules is security - it prevents the possibility of accessing application files outside the webroot folder - something which is not necessary if using a production install. A side effect of this is that urls remain the same whether it's a development-style or production-style install.

Use helper classes

Classes such as the html helper take care of knowing where files are and linking to them appropriately, don't be tempted to hardcode the path to your assets in your template files i.e. don't do this:

<html> <link rel="stylesheet" href="/my_app_name/css/cake.css" /> 

instead do this:

<html> <?= $this->Html->css('forms'); ?> 

Otherwise, you'll find that even something as simple as renaming your application mean links/assets break.

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

Comments

1

If your CSS files are located in /app/webroot/css/ (what is default location) you don't have to know nothing else, include them in your template absolute to webroot:

<link href="/css/cake.css" rel="stylesheet" type="text/css"> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.