How to define a global variable(value) in codeIgniter

前端 未结 4 954
悲&欢浪女
悲&欢浪女 2021-01-22 02:58

I simply do not mean how to define a global variable/constant in CodeIgniter. Let me explain: I have made a theme engine which is select-able from the current logged in user\'s

相关标签:
4条回答
  • 2021-01-22 03:37

    I got this from the manual and this what I have at the top of my config/config.php file: (i have a custom config set to paypal testing)

    // HOW TO USE - For example if there's $config['foo'] = 'bar';
    // in the config 
    // using $this- >config->item('foo') will be 'bar'.
    
    // example for my paypal testing: 
    $config['paypaltest']=0;  
    

    http://ellislab.com/codeigniter%20/user-guide/libraries/config.html

    and how to access in a controller:

    $paypaltest = $this->config->item('paypaltest');
    
    0 讨论(0)
  • 2021-01-22 03:38

    Create A core controller, since your process requires logical operations then you need a method for that.

    application/core/MY_Controller.php

    class MY_Controller Extends CI_Controller
    {
    
       protected $default_theme = 'theme';
    
       public function __construct()
       {
          parent::__construct();
       }
    
       public function get_theme()
       {
             //your code for selecting the current theme selected from
             //the database
             $theme_from_db = '';
    
             return $theme_from_db == NULL ? $this->default_theme : $theme_from_db;
       }
    }
    

    Your Controller must extend MY_Controller

    application/controller/view.php

    class view extends MY_Controller
    {
       public function index()
       {
         $this->load->view($this->get_theme().'result', $data);
       }
    }
    
    0 讨论(0)
  • 2021-01-22 03:39

    in code igniter global constants can be defined in

    config->constants.php
    

    even you no need to load it,it automatically autoloaded by CI automatically.

    0 讨论(0)
  • 2021-01-22 03:52
    In Codeigniter all constant is defined inside application/config/constant.php.
    like: define("CONSTANTNAME","value");
    
    Constant degined here is accessible throughout all pages, ie; controllers, models and views
    
    0 讨论(0)
提交回复
热议问题