CodeIgniter, call a function from one controller to another [closed]

1

Greetings from me, I have a question with CodeIgniter, and it is as follows:

It turns out that I am doing a small system of fines for vehicles, something simple to understand the nature of the framework, where I have 2 tables in a database, (vehicle and fine), this in turn I have 2 models (VehiculoModel and MultaModel), which are accessed by the 2 drivers (Vehicle and Fine), respecting the philosophy of this framework, and I made the section to register, save and consult vehicles, in vehicle controller, with their necessary views, now the doubt is the next ......... I am trying to call from the fine controller a function that is in the vehicle controller, which is to obtain the data of the vehicle and load it in the view that will be in charge of registering the fine, as Can you do this operation?

    
asked by juan 02.05.2016 в 21:25
source

1 answer

1

What you are trying to do is not compatible with the behavior of the MVC system. If you want to execute an action of another controller, you have to redirect the user to the page you want (that is, the function of the controller that consumes the url).

If you want a common functionality, you must build a library that is used in the two controllers.

It can be assumed that you want to build your application a bit modular. (That is, re-use the output of a controller's method in other controller methods.) The simplest way is to use a library to build common "controls" (that is, load the model, render the view in a string) . Then, you can return this string and pass it to the other controller view.

For example:

$string_view = $this->load->view('someview', array('data'=>'stuff'), true);

You can also check the following link that has information on how to do it through routes. link

EDITED QUESTION IN THE COMMENT

To call a model in the library you could do something like this:

public function util(){
      $CI =& get_instance();
      $CI->load->model('VehiculoModel');
      $result = $CI->prueba_model->getVehiculo();
      return $result;

}

    
answered by 02.05.2016 / 22:23
source