如何将包含语言文件的数组变量放在库中或帮助程序中,以便在codeigniter中的每个控制器文件中?

时间:2013-09-10 13:24:38

标签: php arrays codeigniter

我在每个控制器类中都有一个相同的数组公共变量。数组变量包含要传递给视图文件的语言文件。 示例:public $data; $this->data = array('lbl_first_name'=>$this->lang->line('lbl_first_name'));。由于语言数据充足,因此保存语言文件的数组的内容也是如此。如何将此变量放入库或作为帮助程序然后在每个控制器文件中加载类似$this->load->library('language_data') or $this->load->helper('language_data')的内容?不再是在我拥有的每个控制器中加载了大量语言数据的数组变量。非常感谢。示例代码如下所示:

控制器1:

class Courses extends CI_Controller {

 public $data;

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

 $this->data =array(

                    //language file for menu item
                   'dropdown'=>$this->lang->line('dropdown'),
                   'dropdownedit'=>$this->lang->line('dropdownedit'),
                   'home'=>$this->lang->line('home'),
                   'menu_desc'=>$this->lang->line('menu_desc'),
                   'login'=>$this->lang->line('login'),
                   'login_desc'=>$this->lang->line('login_desc'),
                   'teacher'=>$this->lang->line('teacher'),
                   'logout'=>$this->lang->line('logout'),


                   'course_occasion'=>$this->lang->line('course_occasion'),
                   'courses'=>$this->lang->line('courses'),
                   'student'=>$this->lang->line('student'),
                   'tennant'=>$this->lang->line('tennant'),
                   'messages'=>$this->lang->line('messages'),
                   'sent_messages'=>$this->lang->line('sent_messages'),



                    //language file for forms

                   'course_edit_form_desc'=>$this->lang->line('course_edit_form_desc'),
                   'course_reg_form_desc'=>$this->lang->line('course_reg_form_desc'),
                   'course_view_list'=>$this->lang->line('course_view_list'),
                   'view_course_available_list'=>$this->lang->line('view_course_available_list'),
                   'lbl_course_name'=>$this->lang->line('lbl_course_name'),
                   'lbl_course_desc'=>$this->lang->line('lbl_course_desc'),
                   'lbl_tennant_name'=>$this->lang->line('lbl_tennant_name'),
                   'lbl_public'=>$this->lang->line('lbl_public'),
                   'lbl_not_public'=>$this->lang->line('lbl_not_public')
    );
 }
}

控制器2 :(与控制器1相同)

class Occasions extends CI_Controller {

 public $data;

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

 $this->data =array(

                    //language file for menu item
                   'dropdown'=>$this->lang->line('dropdown'),
                   'dropdownedit'=>$this->lang->line('dropdownedit'),
                   'home'=>$this->lang->line('home'),
                   'menu_desc'=>$this->lang->line('menu_desc'),
                   'login'=>$this->lang->line('login'),
                   'login_desc'=>$this->lang->line('login_desc'),
                   'teacher'=>$this->lang->line('teacher'),
                   'logout'=>$this->lang->line('logout'),


                   'course_occasion'=>$this->lang->line('course_occasion'),
                   'courses'=>$this->lang->line('courses'),
                   'student'=>$this->lang->line('student'),
                   'tennant'=>$this->lang->line('tennant'),
                   'messages'=>$this->lang->line('messages'),
                   'sent_messages'=>$this->lang->line('sent_messages'),



                    //language file for forms

                   'course_edit_form_desc'=>$this->lang->line('course_edit_form_desc'),
                   'course_reg_form_desc'=>$this->lang->line('course_reg_form_desc'),
                   'course_view_list'=>$this->lang->line('course_view_list'),
                   'view_course_available_list'=>$this->lang->line('view_course_available_list'),
                   'lbl_course_name'=>$this->lang->line('lbl_course_name'),
                   'lbl_course_desc'=>$this->lang->line('lbl_course_desc'),
                   'lbl_tennant_name'=>$this->lang->line('lbl_tennant_name'),
                   'lbl_public'=>$this->lang->line('lbl_public'),
                   'lbl_not_public'=>$this->lang->line('lbl_not_public')
    );
 }
}

期望的输出:

控制器1和控制器2:

$this->load->library('language_array'); 

$this->load->helper('language_array');

1 个答案:

答案 0 :(得分:1)

我不太清楚我理解你,但我认为这就是你想要的:

首先创建一个库或辅助函数返回lang数组。

创建一个控制器,如:

class MY_Controller extends CI_Controller {

     public $data;

     public function __construct(){
        parent::__construct(); 
        $this->load->library('language_array'); 
    }
}

所以现在你有一个控制器可以加载lib或helper项目吗?

然后是class Occasions extends MY_Controllerclass Courses extends MY_Controller,所以你希望所有控制器都放入MY_Controller所有其他控制器继承的控制器。