简体   繁体   English

如何在cakephp中获取基本URL?

[英]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 , ie 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? 连接样式表的最佳命令是什么?

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 Router class: 顺便说一下,如果你确实需要在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

There are a few different ways to get the base path. 获得基本路径有几种不同的方法。 I use 我用

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

用于baseurl

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

On a related note. 在相关的说明。

If you need the theme url you can do this: 如果您需要主题网址,您可以这样做:

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

You must format: WWW_ROOT . 您必须格式化:WWW_ROOT。 DS . DS。 'css/file.css'; 'CSS / file.css';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM