The base_url
config MUST包含protocol and a 尾部斜杠以及。
来自config.php
file:
您的 CodeIgniter 根目录的 URL。通常这将是您的基本 URL,
带有尾部斜杠:
http://example.com/
如果未设置,则 CodeIgniter 将猜测协议、域
以及安装路径。
因此,您可以设置base_url
如下所示,或者在这种情况下只需将其留空:
$config['base_url'] = 'http://localhost/';
# base_url should be absolute, If you've installed CI in sub-folder:
$config['base_url'] = 'http://localhost/path/to/codeigniter_folder/';
边注:为了使用 URL 辅助函数,例如anchor()
, 加载帮助文件首先:$this->load->helper('url');
(或通过自动加载助手autoload.php
配置文件)。
为什么base_url
配置影响anchor()
功能?
anchor() https://github.com/EllisLab/CodeIgniter/blob/2.1-stable/system/helpers/url_helper.php#L143功能,uses site_url() https://github.com/EllisLab/CodeIgniter/blob/2.1-stable/system/helpers/url_helper.php#L147-L153辅助函数来确定超链接的 URL 地址。
And the site_url() https://github.com/EllisLab/CodeIgniter/blob/2.1-stable/system/helpers/url_helper.php#L45本身,使用两个base_url and index_page https://github.com/EllisLab/CodeIgniter/blob/2.1-stable/system/core/Config.php#L256-L266配置来创建 URL 地址。
因此,如果您分配了错误的值base_url
and/or index_page
配置,anchor()
功能将无法正常工作。