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

时间:2015-03-31 13:00:27

标签: php codeigniter

我在发送到我的视图的不同控制器上有相同的变量,如下所示:

控制器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);

控制器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);

这样做,我总是要将相同的代码复制并粘贴到其他控制器。我这样做没问题。但是,我最大的问题是当我需要修改一个变量时。我必须重新复制 - 并将相同的代码粘贴到其他控制器中多次。如何解决此问题以尽量减少重复?

提前感谢:)

4 个答案:

答案 0 :(得分:2)

我首先将您要共享的数组放在core目录中的父控制器中:

//located in application/core/MY_Parent_Controller.php

class MY_Parent_Controller extends CI_Controller {

    public $shared_data;

    function __construct() {
        parent::__construct();
        $this->shared_data = array(
            'priv_information' => $this->m_user_group->get_priv_information(),
            'priv_customer' => $this->m_user_group->get_priv_customer(),
            'priv_new_model' => $this->m_user_group->get_priv_new_model(),
            'priv_price' => $this->m_user_group->get_priv_price(),
            'priv_masspro' => $this->m_user_group->get_priv_masspro(),
            'priv_product' => $this->m_user_group->get_priv_product(),
            'priv_calendar' => $this->m_user_group->get_priv_calendar(),
            'priv_maintenance' => $this->m_user_group->get_priv_maintenance()
        );
    }
}

然后,控制器A(在常规controllers目录中)将从父

扩展
//located in application/controllers/ContollerA.php

class ContollerA extends MY_Parent_Controller{

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

    function other(){
        $this->shared_data['priv_information'] = 'other info here';
        $this->load->view($this->template, $this->shared_data);
    }

    function another(){
        $this->shared_data['priv_new_model'] = 'other info here';
        $this->load->view($this->template, $this->shared_data);
    }

}

控制器B(在常规controllers目录中)也将从父级扩展

//located in application/controllers/ContollerB.php

class ContollerB extends MY_Parent_Controller{

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

    function other(){
        $this->shared_data['priv_information'] = 'other info here';
        $this->load->view($this->template, $this->shared_data);
    }

    function another(){
        $this->shared_data['priv_new_model'] = 'other info here';
        $this->load->view($this->template, $this->shared_data);
    }

}

答案 1 :(得分:1)

你可以写一个lib:

在应用程序/库中创建Tools.php(或任何你想要的)

<?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;
    }

}

然后在你的控制器中:

 $this->load->library("tools");
 $data = $this->tools->buildData();
 $this->load->view($this->template, $data);

答案 2 :(得分:0)

将代码放在扩展MY_Controller的类中,并重载所需的方法。务必在控制器中调用父级功能。

application/core/MY_Child_class.php

class MY_Child_class extends MY_Controller {

    public function index() {
        // your code here
    }
}

application/controllers/your_controller.php

class Your_controller extends MY_Child_class {
    public function index() {
        parent::index();
        // controller specific code here
    }
}

希望您可以将此概念应用于您的系统。

答案 3 :(得分:0)

全部来自同一型号。所以只需在该模型中创建一个方法然后调用它。这也使代码保持在同一个地方,而不是分散在不同的地方。

控制器

// get the data, if there is an error show appropriate view
if( ! $data = $this->m_user_group->returnCustomer() ){

    $this->showError() ; }

else { $this->load->view($this->template, $data); } 

在m_user_group模型中你可以这样做:

function returnCustomer(){

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

return $data ; 

}

然而这是不好的,因为无论何时查询数据库都应该进行错误检查。然后你有几个选择 - 比如没有值回来时创建默认值

function returnCustomer(){

if( ! $data['priv_information'] = $this->get_priv_information() ){

$data['priv_information'] = 'some default value' ; }

// etc etc 

或者每个db调用必须工作,如果有错误则返回false

function returnCustomer(){

if( ! $data['priv_information'] = $this->get_priv_information() ){

  return false ; }

// etc etc