简体   繁体   English

echo base_url的问题

[英]Problems with echo base_url

I have already loaded the URL helper, I have set the base url in config, yet this code seems to gave me: 我已经加载了URL帮助器,我已经在config中设置了基本URL,但是这段代码似乎给了我:

PHP Error was encountered

Severity: Error

Message: Call to undefined function base_url()

Filename: views/site_navigation.php

Line Number: 6

Here is line number six: 这是第六行:

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

This error means only one thing that you did not load the url helper correctly. 错误 表示您未正确加载url帮助程序的 件事。 Please follow the documentation and load it as specified from your current action or from the constructor of your controller. 请按照文档进行操作,并按照当前操作或控制器构造函数的指定加载

When you need to use the base_url(); 当你需要使用base_url(); or config_item('base_url') or $this->config->item('base_url') config_item('base_url')$this->config->item('base_url')

First example: 第一个例子:

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

Or 要么

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

Second Example: 第二个例子:

Go to application / config / autoload.php 转到application / config / autoload.php

And add the url to here which I think is best for you. 并在此处添加我认为最适合您的网址。

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

Now go to the application / config / config.php Find $config['base_url'] = ''; 现在转到application / config / config.php查找$config['base_url'] = '';

Enter your base url example http://www.example.com/ or http://localhost/project/ 输入您的基本网址示例http://www.example.com/http://localhost/project/

  • $config['base_url'] = 'http://www.example.com/'; Recommended include forward slash at end 推荐包括结尾的正斜杠
  • $config['base_url'] = 'http://localhost/project/'; Recommended include forward slash at end 推荐包括结尾的正斜杠

If you need to remove the index.php find htaccess examples here 如果需要删除index.php,请在此处找到htaccess示例

Download htaccess for codeigniter Git hub 下载htaccess for codeigniter Git hub

Codeigniter User Guides Codeigniter用户指南

Codeigniter User Guides For Version 2 & 3 Can Be Found Here Codeigniter用户指南版本2和3可以在这里找到

Once all that is done 一切都完成了

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

This will get you to the default home page 这将使您进入默认主页

If you are using CI 2.0.0 (in line number 67) 如果您使用的是CI 2.0.0 (第67行)

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

or else if you are using CI 3.0.0 (in line number 91) 或者,如果您使用的是CI 3.0.0 (第91行)

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

in addition 此外

Assign these empty 分配这些空

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

base_url()是对象配置的一个功能试试这个: <li><a href="<?php echo $this->config->base_url(); ?>/welcome">Home</a></li>

Check your 检查你的

CI/system/helpers/url_helper.php CI /系统/助理/ url_helper.php

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

you can set it on 你可以设置它

CI/application/config/config.php CI /应用/配置/ config.php文件

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM