【问题标题】:Explode array into arguments (AJAX wrapper in CodeIgniter)将数组分解为参数(CodeIgniter 中的 AJAX 包装器)
【发布时间】:2011-12-25 16:10:51
【问题描述】:

我正在尝试在 CodeIgniter 中构建 AJAX 包装器

function admin_ajax () {

    $model = $this->input->post('model');
    $method = $this->input->post('method');

    if ( empty($model) || empty($method) )
        die('Missing Class or Method');

    if ( !method_exists($model, $method) )
        die('Class or Method does not exist');

    $args = $this->input->post('args');     
    $data = $this->$model->$method( $args );

    echo json_encode( $data );
    exit();     
}

你大概知道我在做什么。将模型和方法连同一些参数一起发布到该函数,然后该函数调用该函数并传递参数($args 是一个数组 btw)。

假设我有这样的功能:

get_site_list ($page_index = 0, $page_size = 10) {

它需要两个参数,但我调用它的 ajax 函数只能传递一个参数。我不得不恢复做

function ajax_get_site_list ( $args ) {
    return $this->get_site_list( $args[0], $args[1] );
}

但这很无聊。有没有一种方法可以将我的参数数组称为$args 并将它们传递给一个接受多个参数的函数?

【问题讨论】:

  • explode(),implode() 带有“,”分隔符的字符串?

标签: php ajax codeigniter parameters arguments


【解决方案1】:

我想你在找call_user_func_array:

function ajax_get_site_list ( $args ) {
    return call_user_func_array(array($this, 'get_site_list'), $args);
}

ReflectionClass 也能让你非常喜欢;)

$class_name = get_class($this);
$method = new ReflectionMethod($class, 'get_site_list');
$method->invoke($args);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2019-04-26
    • 2016-04-20
    相关资源
    最近更新 更多