dongnong3019 2013-09-10 13:24
浏览 51
已采纳

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

I have an array public variable loaded identically in every controller class that I have. The array variable contains language file to be passed to the view file. Sample:public $data; $this->data = array('lbl_first_name'=>$this->lang->line('lbl_first_name'));. As language data goes plenty, so as the content of the array that holds the language file does also. How would I able to put this variable to a library or as a helper then loads it something like $this->load->library('language_data') or $this->load->helper('language_data') in every controller file? not the array variable with lots of language data anymore to be loaded in each controller I have. Thanks a lot. Sample codes are shown below:

Controller 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')
    );
 }
}

Controller 2: (Same as controller 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')
    );
 }
}

Desired output:

Controller 1 and Controller 2:

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

or

$this->load->helper('language_array');
  • 写回答

1条回答 默认 最新

  • dsz7121 2013-09-10 14:17
    关注

    Not too sure I understand you, but I think this is what you want:

    Firstly create a library or helper function return the array of lang.

    create a controller like:

    class MY_Controller extends CI_Controller {
    
         public $data;
    
         public function __construct(){
            parent::__construct(); 
            $this->load->library('language_array'); 
        }
    }
    

    So now you have a controller which loads you lib or helper item right?

    Then class Occasions extends MY_Controller and class Courses extends MY_Controller, so anything that you want all your controllers to have you put in MY_Controller which all you other controllers inherit from.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀