site_url() 和 base_url() 有什么区别?
正如我在一些资源中读到的,codeigniter
中的 base_url()
和 site_url()
函数几乎相同,尽管我的版本codeigniter (2.1.3) 的 config.php 文件(在 config 目录中)中没有 site_url().
As I have read in some resources, base_url()
and site_url()
functions in codeigniter
are almost the same, although my version of codeigniter (2.1.3) does not have a site_url() in it's config.php file (in config directory).
然而,自从我看到 site_url() 带有参数而从未见过 base_url() 没有参数以来,它们之间是否存在任何差异?
Yet are there differences between them in any way since I have seen site_url() with parameters and never seen base_url() holding none?
echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php
如果你想要一个资源(比如css、js、image)的URL访问,使用base_url()
,否则使用site_url()
更好.
if you want a URL access to a resource (such as css, js, image), use base_url()
, otherwise, site_url()
is better.
详细参考 在 CodeIgniter 中检查这两个函数.
for a detailed reference Check this both function in CodeIgniter.
public function site_url($uri = '')
{
if (empty($uri))
{
return $this->slash_item('base_url').$this->item('index_page');
}
$uri = $this->_uri_string($uri);
if ($this->item('enable_query_strings') === FALSE)
{
$suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
if ($suffix !== '')
{
if (($offset = strpos($uri, '?')) !== FALSE)
{
$uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
}
else
{
$uri .= $suffix;
}
}
return $this->slash_item('base_url').$this->slash_item('index_page').$uri;
}
elseif (strpos($uri, '?') === FALSE)
{
$uri = '?'.$uri;
}
return $this->slash_item('base_url').$this->item('index_page').$uri;
}
基本网址功能.
public function base_url($uri = '')
{
return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
}