Dynamically execute a class model method from an ajax request for the controller

4

I'm creating a feature on my system for reloading components dynamically so that queries are executed that are allocated to model-layer classes through AJAX requests. However, as I use the MVC standard from the CodeIgniter framework, every request or execution must start from a control class, so I can not execute the queries directly through the class model where it is found.

Would anyone know to inform me of a tool or technique that would facilitate this work for me? Is it possible to execute the queries that are in the model securely directly from the AJAX request, without passing in the controller? Or, if not, is there any tool or function that would allow me to dynamically execute the methods of the model classes from a controller for this function?

For testing purposes only, I started to develop a method in a specific controller to dynamically execute the class and the method requested by parameter via request (http or ajax), however this function will be very laborious if it is used in environment since I have to define parameters and treat them according to the type when concatenating in the string, and worse, how they would be passed via url:

public function requestJson($classe, $metodo, $parametros = array()) {

echo "Executando o método <i>{$metodo}</i> a classe <i>{$classe}</i>:<br>";

$str = '$class = new ' . $classe . '();'
                . '$class->' . $metodo . '();';

        return eval($str);
}
    
asked by anonymous 05.01.2015 / 13:15

2 answers

1

The simplest is to call the model method directly

public function requestJson($classe, $metodo, $parametros = array()) {


$var =  "Executando o método <i>{$metodo}</i> a classe <i>{$classe}</i>:<br>";

    return json_encode($this->nome_do_model->requestJson($classe, $metodo, $parametros));
}

And in the Model put the text return

public function requestJson($classe, $metodo, $parametros){
//executa os metodos e retorna para o controller
return "Executando o método <i>{$metodo}</i> a classe <i>{$classe}</i>:<br>";

}

    
22.01.2015 / 19:48
1

What I usually do when I make an Ajax request is to call the controller, the controller requests the data, so I play it in a view or a json to return the data to the ajax request.

html example:

<a href="#" id="exemploAjax">Link Ajax</a>
<div id="respostaAjax"></div>

js example:

$('a#exemploAjax').click(function(e) {
    e.preventDefault();

    $.ajax({
        url: 'meu_controller/meu_metodo',
        type: 'POST',
        success: function(resp)  {

            var resp = $.parseJSON(resp);

            if (resp.success === true) {
                $('#respostaAjax').html(resp.msg);
            }
            else {
                alert(resp.msg);
            }
        }
    });
});

controller example:

class Meu_controller extends MY_Controller {

    public function meu_metodo()
    {
        $this->load->model('meu_model_m');
        $dados['items'] = $this->meu_model_m->get_items();

        $this->load->view('minha_view', $dados);
    }

}

model example:

class Meu_model_m extends MY_Model {

    public function get_items()
    {
        $query = $this->db
            ->select('nome')
            ->get('tabela_items');

        if ($query->num_rows() > 0) {
            return $query->result();
        }

        return array();
    }

}

view example:

<h1>Exemplo Ajax - Lista Items</h1>
<?php
echo '<ul>';
foreach ($items as $item) {
    echo '<li>';
    echo $item->nome;
    echo '</li>';
}
echo '</ul>';
?>
    
07.08.2015 / 22:12