在代码点火器中使用通用功能(控制器)

I am using Code Igniter as the framework for a project I am working on. I am a little new to the MVC world but learning as I go.

In my project, I have a front end controller and a back end admin controller.

On the front end, I have a registration form that has about 5 drop down's that are populated by a database call. In the admin side of things, I show the user submitted record along with pre-selecting the option in the dropdown that was chosen.

In order to do this, I had to also populate the dropdowns in the admin controller.

This is where my question comes into play. In a scenario like that, I essentially have 3 identical functions in the front end controller and the admin controller.

How can I go about creating a common or controller that both of these controllers have access to?

Here is an example of a shared function:

public function fetchSites($org = null)
{
    if (!$org) {
        if (null !== $this->input->post('org')) {
            $sites = $this->support_model->getSites($this->input->post('org'));
            header('Content-Type: application/json');
            echo json_encode($sites);
        }
    } else {
        $sites = $this->support_model->getSites($org);
        return $sites;
    }
}

Both the front end and backend share the same model, just different controllers.

I looked into creating a controller in the application/core but I couldn't figure out how to access the data from that controller.

You can easily create a helper and call it from both controllers, that way you wont be repeating code.

Something along with this in your helper file:

function sharedFunctionality($org = null)
{
   $CI = & get_instance();
   $CI->load->model('path/to/support_model');
   if (!$org){
      $sites = $CI->support_model->getSites($CI->input->post('org'));
      header('Content-Type: application/json');
      echo json_encode($sites);
   } else {
      $sites = $CI->support_model->getSites($org);
      return $sites;
}
}

In both your controllers you will only need to call the helper and then the function:

$this->load->helper('nameOfHelper');
$sites = sharedFunctionality($this->input->post('field'));

More information to create a helper file: https://ellislab.com/codeigniter/user-guide/general/helpers.html