CodeIgniter: get_instance inside My_Lang

江枫思渺然 提交于 2019-12-12 12:27:14

问题


I found this useful Internationalization code:

http://pastebin.com/SyKmPYTX

everything works well except I am unable to use CI functions inside this class .

I want to set $languages and $special variable from DB .

but when I am using $CI =& get_instance(); in instance function its showing following error :

Fatal error: Class 'CI_Controller' not found in /system/core/CodeIgniter.php on line 231


回答1:


The language class is loaded before the CodeIgniter instance exists, which is why you get the error.

You can use a post_controller_constructor hook to set your variables.

Here is a thread from the CodeIgniter forums where someone is tried to do something similar: http://codeigniter.com/forums/viewthread/108639/




回答2:


The easiest way

in My_Lang.php

var $languages = array();

function __construct()
{
parent::__construct();

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->query( 'SELECT * FROM languages');
$result = $query->result();

foreach( $result as $row )
{
$this->languages[$row->short_name] = $row->full_name;
}
}

i did this and is working fine :)) i also added default_uri in foreach.



来源:https://stackoverflow.com/questions/7546241/codeigniter-get-instance-inside-my-lang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!