drjk87189 2015-03-31 13:00
浏览 50
已采纳

PHP CodeIgniter - 为不同的控制器使用相同的功能 - 最小化重复

I have the same variables on different controllers that are sent to my view like this:

Controller A

$data['priv_information'] = $this->m_user_group->get_priv_information();
$data['priv_customer'] = $this->m_user_group->get_priv_customer();
$data['priv_new_model'] = $this->m_user_group->get_priv_new_model();
$data['priv_price'] = $this->m_user_group->get_priv_price();
$data['priv_masspro'] = $this->m_user_group->get_priv_masspro();
$data['priv_product'] = $this->m_user_group->get_priv_product();
$data['priv_calendar'] = $this->m_user_group->get_priv_calendar();
$data['priv_maintenance'] = $this->m_user_group->get_priv_maintenance();
$this->load->view($this->template, $data);

Controller B

$data['priv_information'] = $this->m_user_group->get_priv_information();
$data['priv_customer'] = $this->m_user_group->get_priv_customer();
$data['priv_new_model'] = $this->m_user_group->get_priv_new_model();
$data['priv_price'] = $this->m_user_group->get_priv_price();
$data['priv_masspro'] = $this->m_user_group->get_priv_masspro();
$data['priv_product'] = $this->m_user_group->get_priv_product();
$data['priv_calendar'] = $this->m_user_group->get_priv_calendar();
$data['priv_maintenance'] = $this->m_user_group->get_priv_maintenance();
$this->load->view($this->template, $data);

Doing it this way, I always have to copy and paste the same code to other controllers. I have no problem doing this. However, my big problem is when I need to modify one variable. I have to re-copy - and paste the same code in others controller multiple times. How do I solve this problem to minimize repetition?.

thanks in advance :)

  • 写回答

4条回答 默认 最新

  • dongmuyuan3046 2015-03-31 13:09
    关注

    You can write a lib :

    In application/libraries create Tools.php (or whatever you want)

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Tools
    {
      protected $ci;
    
        public function __construct()
        {
            $this->ci =& get_instance(); 
        }
    
        public function buildData()
        {
           $this->ci->load->model('m_user');
           $data['priv_information'] = $this->ci->m_user_group->get_priv_information(); 
           $data['priv_customer'] = $this->ci->m_user_group->get_priv_customer();   
           $data['priv_new_model'] = $this->ci->m_user_group->get_priv_new_model();  
           $data['priv_price'] = $this->ci->m_user_group->get_priv_price();    
           $data['priv_masspro'] = $this->ci->m_user_group->get_priv_masspro();    
           $data['priv_product'] = $this->ci->m_user_group->get_priv_product();
           $data['priv_calendar'] = $this->ci->m_user_group->get_priv_calendar();
           $data['priv_maintenance'] = $this->ci->m_user_group->get_priv_maintenance();
    
           return $data;
        }
    
    }
    

    And then in your controllers :

     $this->load->library("tools");
     $data = $this->tools->buildData();
     $this->load->view($this->template, $data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集