2016-11-28 56 views

回答

0

要使用base_url(),您必须先加载URL助手。这可以在application/config/autoload.php完成,则:

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

或手动:

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

要打印返回值:

echo base_url(); 
+0

首先在autoload.php中加载url助手。但是当在自定义配置文件(config/my_config.php)上使用base_url()时抛出错误,即未定义的方法base_url() –

1

你的问题不清楚,但给它之前。为了使BASE_URL加载url helper

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

$autoload['config'] = array('custom'); // application > config/custom.php 

的创建自定义配置文件

<?php 

// Testing 
echo config_item('base_url'); 

$config['test'] = '111'; 

上或在__construct区域控制器负荷

检查filenames and classes开始用第一个字母只有大写

<?php 

class Welcome extends CI_Controller { 

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

} 

请确保您已在CodeIgniter 3中设置了您的基本网址,并且建议您使用 以上的版本。

$config['base_url'] = 'http://localhost/project/'; 
0

您可以使用$this->config数组,其中包含默认加载的所有设置。 例如:你可以创建sample.php配置文件,把下面的代码到

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

/* 
| ------------------------------------------------------------------------- 
| Sample 
| ------------------------------------------------------------------------- 
| 
| 
*/ 

//here you can see how is used base_url value from config.php 
$config['r'] = $this->config['base_url']; 

在控制器,你在呼唤它像任何其他配置项:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Welcome extends CI_Controller 
{ 
    public function index() 
    { 
     var_dump($this->config->item('r')); 
    } 
} 

自动加载sample.phpautoload.php文件的配置部分或根据您的应用程序管理加载。

相关问题