CodeIgniter: Where should certain functionality work?

The following is a brief overview of the controller functions in most applications:

  • Controller
  • downloads a specific model, receives data from it, formats the data and transfers the formatted data to the view.

Now there is a search page that should perform a search query across the entire database (all models). It should display each data type in its formatted output on one page in a list.

Problem:

A search controller can search, dynamically load a model for each type of record, and retrieve data from the model. A problem occurs when data needs to be formatted. I am trying to load a specific controller from a search controller, which causes problems.

What to do?

PS: I tried using the Wick library, but it fails when the controller format function tries to use its own model and session object, giving errors in the call for a member on a non-object.

+5
source share
4 answers

After a lot of refactoring and trial / error. It seems like the best way to achieve the above is:

  • Store the format function in the base controller from which all other controllers are derived. Format parameters are passed to the function along with the data object as arguments.

  • Make a static function in each derived controller that returns data formatting options.

  • ( ) , , .

, , , . - , .

+2

, Factory

:

class MyModelFactory {
  static public function Factory($data) {
    $type = key($data);
    return new $type($data);
  }
}

, , - :

$model = MyModelFactory::Factory(array($_REQUEST['model'] => $_REQUEST));

, $_REQUEST ['model']. , , , , .

, , , , /.

class MyModelOne extends Model {
  // stuff
}

, - , , :

abstract class MyAbstractModel extends Model {

  protected $search_params;

  public function __construct($data = array()) {
     $search_params = $data['search_params'];
  }

  protected function GetSearchParameters() {
    return $this->search_params;
  }
  abstract public function GetData();
  abstract public function GetColumns();
  abstract public function DefineViewOptions();
}

class MyModelOne extends MyAbstractModel {


  public function GetData() {
    $params = array();
    $params[] = $this->db->escape_str($this->GetSearchParameters());
    // return whatever data you want, given the search parameter(s)
  }
  public function GetColumns() {
    // return some columns
  }
  public function DefineViewOptions() {
    // return some configuration options
  }
}
0

, CodeIgniter ( , - ).

/. , .

CodeIgniter , .

, , .

CodeIgniter , , - php-.

, , , ( )

0

, , ( , DRY). , CodeIgniter:

  • , , ( , , ). , , .
  • , , , , . SQL, SQL ( SQL ).

PHP: . HTTP, URI, . - , . , JavaScript ( cURL) - , -.

0

All Articles