0

I am using $langs Variable in my Core Controller. How to use Global Variables in Codeigniter? i'm very newbie on CI

My Controller Code is :

class MY_Controller extends CI_Controller{
  //global items
  var $langs;
  // construct
  public function __construct(){
    parent::__construct();
    global $title;
    $langs = array(
      'en' =>'English',
      'zh'  =>  '繁體中文',
      'zh_CN' =>  '簡體中文',
      'fr'  =>  'français',
      'de'  =>  'Deutsch',
      'ja'  =>  '日本語',
      'es'  =>  'Español',
      'pt'  =>  'Português',
      'ru'  =>  'Русский',
      'tr'  =>  'Türkçe',
      'kr'  =>  '한국어'
    );
  }

My View Code is :

<?
//display
var_dump($langs);
?>
0

1 Answer 1

3
class MY_Controller extends CI_Controller
{
    //global items
    public $langs;
    public $title;
    // construct
    public function __construct()
    {
        parent::__construct();

        $langs = array(
            'en' =>'English',
            'zh'  =>  '繁體中文',
            'zh_CN' =>  '簡體中文',
            'fr'  =>  'français',
            'de'  =>  'Deutsch',
            'ja'  =>  '日本語',
            'es'  =>  'Español',
            'pt'  =>  'Português',
            'ru'  =>  'Русский',
            'tr'  =>  'Türkçe',
            'kr'  =>  '한국어'
        );
        $this->langs = $langs;
    }
}

Use it in controllers that are extending MY_Controller as $this->title and $this->langs. If you need it more widely available even if request controller doesn't extends MY_Controller, make own library with similar code and autoload it. Pay attention on making libraries. In docs it is well explained (check that Example_library).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.