Sony Elgadri
Sony Elgadri

Reputation: 1

PHP CodeIgniter - use same functions for different controllers - minimize repetition

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 :)

Upvotes: 0

Views: 913

Answers (4)

AdrienXL
AdrienXL

Reputation: 3008

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);

Upvotes: 1

cartalot
cartalot

Reputation: 3148

its all from the same model. so just make a method in that model and then call it. this also keeps the code in the same place instead of spread out in different places.

controller

// 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); } 

in m_user_group model you could just do this:

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 ; 

}

however that is bad because whenever you are querying a database there should be error checking. you then have a few choices - like create a default if no value comes back

function returnCustomer(){

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

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

// etc etc 

or if every db call has to work then return false if there is an error

function returnCustomer(){

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

  return false ; }

// etc etc 

Upvotes: 0

CodeGodie
CodeGodie

Reputation: 12132

I would first place whatever array you want to share in a parent controller inside your core directory:

//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()
        );
    }
}

Then Controller A (inside your regular controllers directory) would extend from the parent

//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);
    }

}

And Controller B (inside your regular controllers directory) would extend from the parent as well

//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);
    }

}

Upvotes: 2

Harry
Harry

Reputation: 2526

Place the code in a class that extends MY_Controller and overloads the methods that you need. Be sure to call the parent's function in your 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
    }
}

Hopefully you can apply this concept to you system.

Upvotes: 0

Related Questions