ctechhindi / CodeIgniter-API-Controller

CodeIgniter Restful API Controller - Easily build REST API with Token Authorization
MIT License
67 stars 50 forks source link

Method not found show default 404 page #1

Closed anismagdouli19 closed 5 years ago

anismagdouli19 commented 6 years ago

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); } }

anismagdouli19 commented 6 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;
    }
}
islam-devphp commented 5 years ago

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...

jeevan15498 commented 5 years ago

check out documentation : https://github.com/ctechhindi/CodeIgniter-API-Controller#setup-api-request-methods

vishnugbs commented 5 years ago

@islam-devphp , Is the issue solved? { "status": false, "error": "Unknown method" } I'm having same error. @jeevan15498 I did as per the documentation.