CodeIgniter - accessing $config variable in view

后端 未结 11 1045
青春惊慌失措
青春惊慌失措 2020-12-23 12:52

Pretty often I need to access $config variables in views. I know I can pass them from controller to load->view(). But it seems excessive to do i

相关标签:
11条回答
  • 2020-12-23 13:41

    Your controller should collect all the information from databases, configs, etc. There are many good reasons to stick to this. One good reason is that this will allow you to change the source of that information quite easily and not have to make any changes to your views.

    0 讨论(0)
  • 2020-12-23 13:43
    echo $this->config->config['ur config file'] 
    

    If your config file also come to picture you have to access like this for example I include an app.php in config folder I have a variable

    $config['50001'] = "your  message"   
    

    Now I want access in my controller or model .

    Try following two cases one should work

    case1:

    $msg = $this->config->item('ur config file');
    
    echo $msg['50001'];    //out put:  "your message";
    

    case2:

     $msg = $this->config->item('50001');
    
     echo $msg;    //out put:  "your message"
    
    0 讨论(0)
  • 2020-12-23 13:43

    Example, if you have:

    $config['base_url'] = 'www.example.com'
    

    set in your config.php then

    echo base_url();
    

    This works very well almost at every place.
    /* Edit */
    This might work for the latest versions of codeigniter (4 and above).

    0 讨论(0)
  • 2020-12-23 13:49

    If you are trying to accessing config variable into controller than use

    $this->config->item('{variable name which you define into config}');
    

    If you are trying to accessing the config variable into outside the controller(helper/hooks) then use

    $mms = get_instance();  
    $mms->config->item('{variable which you define into config}');
    
    0 讨论(0)
  • 2020-12-23 13:50

    Also, the Common function config_item() works pretty much everywhere throughout the CodeIgniter instance. Controllers, models, views, libraries, helpers, hooks, whatever.

    0 讨论(0)
提交回复
热议问题