使用可选参数重新映射codeigniter索引方法

时间:2012-12-24 14:58:54

标签: codeigniter remap codeigniter-routing

在我的codeigniter控制器中,我只有带可选参数的索引函数。如果参数存在,我加载一个视图,否则我加载另一个。我使用这个_remap函数:

function _remap($method){
    $param_offset = 2;

    // Default to index
    if ( ! method_exists($this, $method))
    {
        // We need one more param
        $param_offset = 1;
        $method = 'index';
    }

    // Since all we get is $method, load up everything else in the URI
    $params = array_slice($this->uri->rsegment_array(), $param_offset);

    // Call the determined method with all params
    call_user_func_array(array($this, $method), $params);
} 

问题在于,当我检查索引函数中的参数时,它总是将值等于0,这是我设置为默认值的值。

1 个答案:

答案 0 :(得分:3)

这是_remap()函数的原始签名:

function _remap($method, $params = array());

查看$params数组,而不是使用URI库,您就可以了。

相关问题