codeigniter访问控制器变量在config中

时间:2016-07-29 11:36:12

标签: php codeigniter

我想做这样的事情

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

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| WARNING: You MUST set this value!
|
| If it is not set, then CodeIgniter will try guess the protocol and path
| your installation, but due to security concerns the hostname will be set
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/
echo $code;
$config['base_url'] = 'http://localhost/';

现在在我的家庭控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
public function index(){

$x=1;
if($x==1){
$this->config->set_item('code', 'somevalue1');
}else if($x==2){
$this->config->set_item('code', 'somevalue2');
}
}
}

现在我如何从Controller的home文件中访问值到application / config / config.php

1 个答案:

答案 0 :(得分:0)

在Config.php中

$config['code']=''; # Its better this to define at end of config file

设置值

$this->config->set_item('code', 'My Value');

访问值

echo $this->config->item('code');