0

我在我拥有的每个控制器类中都加载了一个相同的数组公共变量。数组变量包含要传递给视图文件的语言文件。示例: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');
4

1 回答 1

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其中所有其他控制器都继承自其中的任何内容。

于 2013-09-10T14:17:27.047 回答