-1

In CodeIgniter framework i have two controller files: controllerA.php and controllerB.php

I need controllerB.php to add code in controllerA.php function

I have no idea how to do it, i checked Codeigniter manual, google and stackoverflow but was not able to find solution

controllerA.php has function:

function get_permission_conditions()
{
    return do_action('staff_permissions_conditions', [
        'contracts' => [
            'view'     => true,
            'view_own' => true,
            'edit'     => true,
            'create'   => true,
            'delete'   => true,

]);
}

I want controllerB.php to communicate with controllerA.php and add custom code example:

function get_permission_conditions()
{

//Code from controllerA.php
    return do_action('staff_permissions_conditions', [
        'contracts' => [
            'view'     => true,
            'view_own' => true,
            'edit'     => true,
            'create'   => true,
            'delete'   => true,

//custom code from controllerB.php goes here

]);
}
Back Office
  • 71
  • 1
  • 13
  • I think this link is userful for this. https://stackoverflow.com/questions/14165895/how-to-load-a-controller-from-another-controller-in-codeigniter Here show method of controller class extends to use function of other controller. – Bhavin Thummar Dec 28 '18 at 12:39
  • What is `do_action()`? Where does it come from? – DFriend Dec 28 '18 at 14:14
  • what ever you are doing here - use a model / library for such purpose – Atural Dec 28 '18 at 14:31

2 Answers2

0

You should extend controllerA like this:

class controllerA extends CI_Controller{
    // All the function will go here
    return do_action('staff_permissions_conditions', [
        'contracts' => [
            'view'     => true,
            'view_own' => true,
            'edit'     => true,
            'create'   => true,
            'delete'   => true,

    ]); 
}
class controllerB extends controllerA{
    public $permission_array;
    function __construct() {
        $this->permission_array = $this->do_action();  // Here $permission_array will have the array returned by controllerA's function 'do_action'
    }

    //custom code from controllerB.php goes here
    // You can use $permission all over
}
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

Create a parent controller for both of the and let them both extend that parent and move that method to that parent and let the parent extend CI_Controller .. pretty much like what you did with MY_Controller ...

That's your C_Controller (Parent controller):

class C_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    // your methods that will be extended
    ...

}

That's your A_Controller:

class A_Controller extends C_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}

That's your B_Controller:

class B_Controller extends C_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}
Sherif Salah
  • 2,085
  • 2
  • 9
  • 21