Amjith
Amjith

Reputation: 462

How to get absolute path for css and script

How to get absolute path for stylesheet or javascript files.

<?php echo $this->Html->css('/css/main.css');?>

Is giving URL

<link rel="stylesheet" type="text/css" href="/dev/theme/Default/css/main.css" />

And I am looking for

<link rel="stylesheet" type="text/css" href="http://localhost/dev/theme/Default/css/main.css" />

Upvotes: 0

Views: 3819

Answers (1)

thaJeztah
thaJeztah

Reputation: 29017

I'm really wondering why you would want to do so if the CSS files are served on the same domain as your website, but this will probably work;

<?php echo $this->Html->css($this->Html->url('/css/main.css', true));?>

HtmlHelper::url() (inherited from Helper::url() takes two arguments. The first is the path as a string or array (to make use of cakephp routing), if a Boolean 'true' is passed as its second argument, a 'full' URL is generated.

I'm not at my computer at the moment, so haven't been able to test it, but I think it should work.

update

I had a quick look at the source: https://github.com/cakephp/cakephp/blob/master/lib/Cake/View/Helper/HtmlHelper.php#L431

and HtmlHelper::css() uses Helper::assetUrl() internally;

https://github.com/cakephp/cakephp/blob/master/lib/Cake/View/Helper.php#L305

So this may work as well and if it works, it's a cleaner solution:

<?php echo $this->Html->css('main', null, array('fullBase' => true));?>

Upvotes: 4

Related Questions