what is the difference between site_url() and base

2019-01-07 10:20发布

问题:

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).

Yet are there differences between them in any way since I have seen site_url() with parameters and never seen base_url() holding none?

回答1:

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), '/');
        }


回答2:

site_url: Returns base_url + index_page + uri_string

base_url: Returns base_url + uri_string

See source code of both functions at: https://github.com/EllisLab/CodeIgniter/blob/606fee0e2e0aa6a906db82e77090e91f133d7378/system/core/Config.php



回答3:

1. site_url

The purpose of site_url is that your pages become more portable in the event your URL changes.The site_url appears with the index.php file.

Segments can be optionally passed to the function as a string or an array.

echo site_url("news/local/123");

it will give: http://ci.com/index.php/news/local/123

you can even pass segments as an array:

$segments = array('news', 'local', '123');
echo site_url($segments);

2. base_url

base_url is without the index_page or url_suffix being appended. like site_url, you can supply segments as a string or an array.

If you want a URL access to a resource use base_url you can supply a string to a file, such as an image or stylesheet else site_url is enough.

echo base_url("/images/icons/image.png");


回答4:

site_url() is execute index.php that why site_url is better as compare to base_url.



回答5:

Or, you can create file .htaccess in the CodeIgniter root folder next to its index.php file.

just add this code on .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

this code will make the word index.php remain unseen.

http://localhost/CodeIgniter/index.php/controllersName/

will be changed to this

http://localhost/CodeIgniter/controllersName/


回答6:

base_url() that is commonly used in Codeigniter can be used even without the .php extension.

site_url() which is commonly used in Wordpress custom template can be used even when you just call the post_title of the blog or the file name with the extension.



回答7:

I would like to add when to use base_url() and the site_url() . Basically one can use site_url() while creating links for controllers whereas base_url() can be used where we need to create urls for the assets like loading a css or js file or some image .

What I always prefer is to use site_url() for creating links to controllers or ajax urls and base_url() for loading assets .