0

I have an method in Code Igniter, and want access using this URL "kuesioner/test/school_id/6/parent_id/7" I using _remap function to dynamic order of params in URL. Code from this. This is my code :

public function test($school_id, $parent_id)
{
    echo "<pre>";
    print_r($schoold_id);
    print_r($parent_id);
    echo "</pre>";
}

public function _remap($method, $params)
{
    $map = array();
    for( $i = 1; $i < count( $params ); $i = $i + 2 )
    {
        $map[$params[$i-1]] = $params[$i];
    }

    if( $method[0] != '_' && method_exists( $this, $method ))
        return $this->$method($map);
}

Getting error on my controller that said Missing argument 2 for Kuesioner::test() ($parent_id is missing), it return array of map into single variable $school_id in test controller.

print_r($school_id) exactly show

  Array
  (
    [school_id] => 6
    [parent_id] => 7
  )

How to solve this, so that value can be put into every right variable..

4

4 回答 4

1

始终为参数分配默认值,例如 0 或其他任何可以防止不必要的警告和错误的值。

public function test($school_id = 0, $parent_id = 0)
{
    echo "<pre>";
    print_r($schoold_id);
    print_r($parent_id);
    echo "</pre>";
}
于 2013-05-14T03:53:00.293 回答
0

尝试用这个替换最后一行:

return call_user_func_array(array($this, $method), $map);

我认为这可以解决您的问题,但是如果没有,您可以将“测试”功能编辑为:

public function test($params)
{
  echo "<pre>";
  print_r($params['schoold_id']);
  print_r($params['parent_id']);
  echo "</pre>";
}
于 2013-09-12T03:07:10.177 回答
0
public function test($params = NULL)
{
    print_r($this->uri->uri_to_assoc(3));
    // #CHECK http://ellislab.com/codeigniter/user-guide/libraries/uri.html
}
于 2013-05-14T06:55:01.750 回答
-1

为什么你不尝试使用 $this->uri->uri_to_assoc(n)。查看详细信息:http : //ellislab.com/codeigniter/user-guide/libraries/uri.html 不需要使用_remap

于 2013-05-14T04:05:51.637 回答