what is the difference between site_url() and base_url()?

前端 未结 7 2119
南旧
南旧 2020-11-29 03:09

As I have read in some resources, base_url() and site_url() functions in codeigniter are almost the same, although my version of code

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 03:39

    echo base_url(); // http://example.com/website
    echo site_url(); // http://example.com/website/index.php
    

    if you want a URL access to a resource (such as css, js, image), use base_url(), otherwise, site_url() is better.

    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;
        }
    

    Base URL function.

    public function base_url($uri = '')
            {
                return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
            }
    

提交回复
热议问题