我想訪問base_url()
方法,以便我可以在自定義配置文件中獲取我的應用程序url。在CodeIgniter的自定義配置文件中訪問base_url()或site_url()
我不想在自定義配置文件中編寫$config['base_url'] = 'some url';
。
我想訪問base_url()
方法,以便我可以在自定義配置文件中獲取我的應用程序url。在CodeIgniter的自定義配置文件中訪問base_url()或site_url()
我不想在自定義配置文件中編寫$config['base_url'] = 'some url';
。
要使用base_url(),您必須先加載URL助手。這可以在application/config/autoload.php
完成,則:
$autoload['helper'] = array('url');
或手動:
$this->load->helper('url');
要打印返回值:
echo base_url();
首先在autoload.php中加載url助手。但是當在自定義配置文件(config/my_config.php)上使用base_url()時拋出錯誤,即未定義的方法base_url() –
你的問題不清楚,但給它之前。爲了使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/';
您可以使用$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.php
在autoload.php
文件的配置部分或根據您的應用程序管理加載。
試試'$ this-> config-> item('base_url')'。 – Tpojka
這不起作用 –
@MSaikat已添加我的答案 – user4419336