我想访问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