site_url() 和 base_url() 有什么区别? [英] what is the difference between site_url() and base_url()?

查看:39
本文介绍了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), '/');
        }

这篇关于site_url() 和 base_url() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆