How to get the base Url in cakephp?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 17:38:34

问题


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:


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



回答2:


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

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



回答3:


Use this for baseurl

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



回答4:


On a related note.

If you need the theme url you can do this:

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



回答5:


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



来源:https://stackoverflow.com/questions/13738711/how-to-get-the-base-url-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!