0

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" />

1 Answer 1

4

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));?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.