Rodrigo Souza
Rodrigo Souza

Reputation: 7332

How to get the base Url in cakephp?

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?

Upvotes: 12

Views: 53079

Answers (5)

Vaibhav Shahu
Vaibhav Shahu

Reputation: 380

Use this for baseurl

echo $this->html->url('/', true);

Upvotes: 6

user2214236
user2214236

Reputation: 173

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

Upvotes: -1

Dieter Gribnitz
Dieter Gribnitz

Reputation: 5208

On a related note.

If you need the theme url you can do this:

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

Upvotes: 4

Tim Joyce
Tim Joyce

Reputation: 4517

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

echo $this->webroot; //Note: auto appends trailing slash

Upvotes: 6

nIcO
nIcO

Reputation: 5001

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

Upvotes: 39

Related Questions