【问题标题】:Problems with echo base_urlecho base_url 的问题
【发布时间】:2015-07-09 12:49:49
【问题描述】:

我已经加载了 URL 帮助程序,我已经在 config 中设置了基本 url,但是这段代码似乎给了我:

PHP Error was encountered

Severity: Error

Message: Call to undefined function base_url()

Filename: views/site_navigation.php

Line Number: 6

这里是第六行:

<li><a href="<?php echo base_url(); ?>/welcome">Home</a></li> 

【问题讨论】:

  • @tanaydin 这是一个 CodeIgniter 函数 - 可能是更相关的副本:stackoverflow.com/questions/6449386/…
  • 你能在控制器中显示你的配置文件和你的 URL 助手吗?从我所看到的看起来不错,但可能是您错过了什么导致它出错。
  • 你怎么能加载助手?!
  • 只需在自动加载中添加 url 助手

标签: php codeigniter


【解决方案1】:

这个错误意味着只有一个你没有正确加载url helper的事情。请按照您当前操作或控制器构造函数中指定的文档和load it 进行操作。

【讨论】:

    【解决方案2】:

    当您需要使用base_url();config_item('base_url')$this-&gt;config-&gt;item('base_url')

    第一个例子:

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
    }
    

    或者

    public function index() {
       $this->load->helper('url');
    }
    

    第二个例子:

    转到application/config/autoload.php

    并将我认为最适合您的网址添加到此处。

    $autoload['helper'] = array(
        'url',
        'file',
        'form',
        'text',
        'html',
        'date'
    );
    

    现在去application/config/config.php找到$config['base_url'] = '';

    输入您的基本网址示例http://www.example.com/http://localhost/project/

    • $config['base_url'] = 'http://www.example.com/'; 建议在末尾包含正斜杠
    • $config['base_url'] = 'http://localhost/project/'; 推荐在末尾包含正斜杠

    如果您需要删除 index.php,请在此处查找 htaccess 示例

    Download htaccess for codeigniter Git hub

    Codeigniter 用户指南

    Codeigniter User Guides For Version 2 & 3 Can Be Found Here

    全部完成后

    &lt;li&gt;&lt;a href="&lt;?php echo base_url(); ?&gt;"&gt;Home&lt;/a&gt;&lt;/li&gt;

    这将带您进入默认主页

    【讨论】:

      【解决方案3】:

      如果您使用的是 CI 2.0.0(在第 67 行)

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

      否则,如果您使用的是 CI 3.0.0(在第 91 行)

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

      另外

      分配这些空

      $config['base_url'] = '';
      $config['index_page'] = '';
      

      【讨论】:

        【解决方案4】:

        base_url() 是对象配置的函数 试试这个:&lt;li&gt;&lt;a href="&lt;?php echo $this-&gt;config-&gt;base_url(); ?&gt;/welcome"&gt;Home&lt;/a&gt;&lt;/li&gt;

        【讨论】:

        • 这不再给我一个错误,但是现在当我单击链接时,它会将我定向到“此网页不可用”,我检查了页面源并且链接中的 URL 为他们应该是。
        • 确保config.php文件中的site_url变量设置正确,然后使用$this->config->site_url();
        • 站点 url 的 config.php 文件中没有变量?
        • 我应该粘贴'$this->config->site_url(); ' 到 config.php 文件还是在视图中?
        • 对不起,我的错。验证 $config['base_url'] 变量
        【解决方案5】:

        检查你的

        CI/system/helpers/url_helper.php

        function base_url($uri = '', $protocol = NULL)
        {
            return get_instance()->config->base_url($uri, $protocol);
        }
        

        你可以设置它

        CI/application/config/config.php

        $config['base_url'] = ....
        

        【讨论】:

          【解决方案6】:

          打开 config.php $config['base_url']= "localhost/meusite 以打开 Autoload library('url') 或控制器 $this-&gt;load-&gt;library('url')

          【讨论】:

            猜你喜欢
            • 2016-12-02
            • 1970-01-01
            • 2014-06-01
            • 1970-01-01
            • 1970-01-01
            • 2016-12-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多