在CodeIgniter的自定义配置文件中访问base_url()或site_url()

时间:2016-11-28 19:23:18

标签: codeigniter

我想访问base_url()方法,以便我可以在自定义配置文件中获取我的应用程序URL。

我不想在自定义配置文件中写$config['base_url'] = 'some url';

3 个答案:

答案 0 :(得分: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/'; 

答案 1 :(得分:0)

要使用base_url(),必须先加载URL Helper。这可以在application/config/autoload.php中完成,然后:

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

或手动:

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

打印返回值:

echo base_url();

答案 2 :(得分: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.php文件的配置部分中自动加载autoload.php或根据您的应用管理加载。