2016-11-28 60 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文件的配置部分或根據您的應用程序管理加載。

相關問題