php 如何在cakephp中获取基本网址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13738711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the base Url in cakephp?
提问by Rodrigo Souza
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
我正在使用Html Helper css()方法来像这样链接我的样式表:<?php echo $this->Html->css('reset.css');?>但是如果我的 CakePHP 应用程序是通过除 之外的路径访问的http://site.domain.com,即http://site.domain.com/my_app
What would be the best command to link my stylesheet?
链接我的样式表的最佳命令是什么?
回答by nIcO
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.
如果给定的路径'reset.css'不以斜杠开头,它会自动将路径添加到 CSS 文件夹。
By the way, if you do need to get the base url in Cake, you can use the Routerclass:
顺便说一句,如果您确实需要在 Cake 中获取基本 url,您可以使用Router该类:
//with http://site.domain.com/my_app
echo Router::url('/') //-> /my_app
echo Router::url('/', true) //-> http://site.domain.com/my_app
回答by Vaibhav Shahu
Use this for baseurl
将此用于 baseurl
echo $this->html->url('/', true);
回答by Tim Joyce
There are a few different ways to get the base path. I use
有几种不同的方法可以获取基本路径。我用
echo $this->webroot; //Note: auto appends trailing slash
回答by Dieter Gribnitz
On a related note.
在相关说明上。
If you need the theme url you can do this:
如果您需要主题网址,您可以这样做:
$this->webroot.'theme/'.$this->theme
回答by user2214236
You must format: WWW_ROOT . DS . 'css/file.css';
您必须格式化: WWW_ROOT 。。'css/file.css';

