12

I'm using Html Helper css() method to link my stylesheets just like this: <?php echo $this->Html->css('reset.css');?> but what if my CakePHP app is accessed through a path other than http://site.domain.com, i.e. http://site.domain.com/my_app

What would be the best command to link my stylesheet?

1

5 Answers 5

39

The exact same command should work:

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

It automatically adds the path to the CSS folder if the given path 'reset.css' doesn't start with a slash.

By the way, if you do need to get the base url in Cake, you can use the Router class:

//with http://site.domain.com/my_app
echo Router::url('/')       //-> /my_app
echo Router::url('/', true) //-> http://site.domain.com/my_app
6

There are a few different ways to get the base path. I use

echo $this->webroot; //Note: auto appends trailing slash
2
  • What if this only returns a URL of / instead of /app/webroot?
    – mtpultz
    Commented Mar 17, 2015 at 21:17
  • Will work only at controller level. check @nIcO answer
    – Fr0zenFyr
    Commented Jan 4, 2019 at 8:03
6

Use this for baseurl

echo $this->html->url('/', true);
1
  • Will work only at view level. check @nIcO answer for generic solution
    – Fr0zenFyr
    Commented Jan 4, 2019 at 8:02
4

On a related note.

If you need the theme url you can do this:

$this->webroot.'theme/'.$this->theme
-1

You must format: WWW_ROOT . DS . 'css/file.css';

1
  • 1
    WWW_ROOT have the full path route to webroot, not the url. Commented Jan 10, 2014 at 11:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.