php site_url() 和 base_url() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17079711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:13:22  来源:igfitidea点击:

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

phpcodeigniter

提问by Afghan Dev

As I have read in some resources, base_url()and site_url()functions in codeigniterare 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).

正如我在一些资源已阅读,base_url()site_url()功能codeigniter几乎是一样的,虽然我的版本笨的(2.1.3)没有在它的config.php文件一SITE_URL()(在config目录)。

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

然而,自从我见过带有参数的 site_url() 并且从未见过 base_url() 不带参数以来,它们之间是否存在任何差异?

回答by Fasil kk

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.

如果你想要一个资源(比如css、js、image)的URL访问,使用base_url(),否则site_url()更好。

for a detailed reference Check this both function in CodeIgniter.

有关详细参考,请检查 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), '/');
        }

回答by sikander

site_url: Returns base_url + index_page + uri_string

site_url: 返回 base_url + index_page + uri_string

base_url: Returns base_url + uri_string

base_url: 返回 base_url + uri_string

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

查看这两个函数的源代码:https: //github.com/EllisLab/CodeIgniter/blob/606fee0e2e0aa6a906db82e77090e91f133d7378/system/core/Config.php

回答by Akhilesh krishnan

1. site_url

1.site_url

The purpose of site_urlis that your pages become more portable in the event your URL changes.The site_urlappears with the index.phpfile.

目的site_url是在您的 URL更改时您的页面变得更加便携。site_urlindex.php文件随文件一起出现。

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

它会给: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

2. base_url

base_urlis without the index_pageor url_suffixbeing appended. like site_url, you can supply segments as a string or an array.

base_url没有index_pageurl_suffix被附加。像site_url,您可以将段作为字符串或数组提供。

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

如果您想要对资源使用的 URL 访问,base_url您可以为文件提供一个字符串,例如图像或样式表,其他site_url就足够了。

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

回答by Ulett

Or, you can create file .htaccessin the CodeIgniter root folder next to its index.phpfile.

或者,您可以.htaccess在其文件旁边的 CodeIgniter 根文件夹中创建index.php文件。

just add this code on .htaccessfile:

只需在.htaccess文件中添加此代码:

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

this code will make the word index.phpremain unseen.

此代码将使该词index.php保持不可见。

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

will be changed to this

会改成这个

http://localhost/CodeIgniter/controllersName/

回答by Muhammad Zohaib

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

site_url() 是执行 index.php,为什么 site_url 比 base_url 更好。

回答by TheVigilant

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 .

我想添加何时使用 base_url() 和 site_url() 。基本上可以在为控制器创建链接时使用 site_url() 而 base_url() 可以用于我们需要为资产创建 url 的地方,例如加载 css 或 js 文件或一些图像。

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

我总是喜欢使用 site_url() 来创建指向控制器或 ajax url 的链接,并使用 base_url() 来加载资产。

回答by Archangel08

base_url()that is commonly used in Codeignitercan be used even without the .phpextension.

base_url()Codeigniter即使没有.php扩展名也可以使用常用的。

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

site_url()Wordpress即使您只调用post_title博客的 或带有扩展名的文件名,也可以使用自定义模板中常用的。