Closed anismagdouli19 closed 5 years ago
RE : If we are calling method with parameters we have a default php error so i recomment to add this to api controller :
function _remap($method,$params = array())
{
if(method_exists($this,$method))
{
$required_parameter = $this->get_func_argNames($method);
if(count($params)< count($required_parameter)){
$required_parameter_text = '(';
$i = 0;
foreach ($required_parameter as $key => $value) {
$i++;
if($i == count($required_parameter)){
$required_parameter_text .= $key ;
}else{
$required_parameter_text .= $key . ',' ;
}
}
$required_parameter_text .= ')';
$this->api_return(['error'=> 'You should define Parameters '.$required_parameter_text], self::HTTP_NOT_FOUND);
return false;
}
if(!empty($params)){
return call_user_func_array(array($this, $method), $params);
}else{
call_user_func(array($this, $method));
}
return false;
}
else
{
$this->api_return(['error'=> 'Method Not Found'], self::HTTP_NOT_FOUND);
}
}
private function get_func_argNames($funcName) {
$r = new ReflectionMethod($this->router->fetch_class(), $funcName);
$params = $r->getParameters();
$parameters = [];
foreach ($params as $param) {
//$param is an instance of ReflectionParameter
// echo $param->getName();
// echo $param->isOptional();
if(!$param->isOptional()){
$parameters[$param->getName()] = $param->isOptional();
}
}
return $parameters;
}
}
hi i have an error when i upload the API try to connect to any api give me { "status": false, "error": "Unknown method" } can any one help me with that pleas...
check out documentation : https://github.com/ctechhindi/CodeIgniter-API-Controller#setup-api-request-methods
@islam-devphp , Is the issue solved? { "status": false, "error": "Unknown method" } I'm having same error. @jeevan15498 I did as per the documentation.
I recommend To Use the remap function in the api_controller : function _remap($method,$params = array()) { if(method_exists($this,$method)) { return call_user_func_array(array($this, $method), $params); return false; } else { $this->api_return(['error'=> 'Method Not Found'], self::HTTP_NOT_FOUND); } }