在CodeIgniter 2.*中如何获取基本URL

40
在config.php文件中。
$config['base_url'] = 'http://localhost/codeigniter/';
在视图中
<link rel="stylesheet" href="<?php base_url(); ?>css/default.css" type="text/css" />

=> 错误: 调用未定义函数 base_url(); 帮我


1
你忘记了echo href="<?php echo base_url(); ?>",尝试这个,它适合你。 - pratik
5个回答

95

要使用base_url()快捷方式,首先必须加载URL Helper

$this->load->helper('url');

或者您可以通过更改application/config/autoload.php文件来自动加载它。

或者只需使用

$this->config->base_url();

site_url()也适用于此。

另外我发现你漏掉了echo(虽然这不是你当前的问题),使用下面的代码来解决这个问题。

<link rel="stylesheet" href="<?php echo base_url(); ?>css/default.css" type="text/css" />

如果我在控制器中只使用 $this->config->base_url("localhost/mysite");,但仍然出现“致命错误:调用未定义的函数 base_url()”,这是什么问题?我正在使用 CodeIgniter_2.1.4。 - DeLe

9

我知道这很晚,但对于新手来说很有用。我们可以自动加载url助手,并在整个应用程序中使用它。为此,在application\config\autoload.php中进行以下修改 -

$autoload['helper'] = array('url'); 

7

为了使用base_url(),您需要加载URL Helper。在您的控制器中,执行以下操作:

$this->load->helper('url');

然后在您的视图中,您可以执行以下操作:

echo base_url();

4

只需加载帮助类

$this->load->helper('url');

就这样。


0

你需要在config/autoload中添加url helper

$autoload['helper'] = array('form', 'url', 'file', 'html'); <-- Like This

然后你可以使用base_url或任何类型的url。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接