질문 (내가 요청한 방식)이 올바른지 여부를 모르겠습니다. 나는 당신의 제안에 열려 있습니다. 다음 코드가 정확히 어떻게 작동하는지 알고 싶습니다. 자세한 내용을 원하시면 원하는만큼 제공 할 수 있습니다.
public function processAPI() {
if (method_exists($this, $this->endpoint)) {
return $this->_response($this->{$this->endpoint}($this->args));
}
return $this->_response("No Endpoint: $this->endpoint", 404);
}
private function _response($data, $status = 200) {
header("HTTP/1.1 " . $status . " " . $this->_requestStatus($status));
return json_encode($data);
}
private function _requestStatus($code) {
$status = array(
200 => 'OK',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
);
return ($status[$code])?$status[$code]:$status[500];
}
/**
* Example of an Endpoint
*/
protected function myMethod() {
if ($this->method == 'GET') {
return "Your name is " . $this->User->name;
} else {
return "Only accepts GET requests";
}
}
여기 $this->endpoint is 'myMethod' (a method I want to execute)
URL에서 실행하려는 메서드를 전달합니다. 이 함수는 요청 프로세스를 포착 한 다음 정확한 메서드를 호출합니다. 어떻게 작동하는지 알고 싶습니다. 특히이 라인.
return $this->_response($this->{$this->endpoint}($this->args));
PHP는 변수 함수 와 변수 변수를 모두 지원 합니다 .
processApi 내에서 진술에 도달하면
return $this->_response($this->{$this->endpoint}($this->args));
PHP는 엔드 포인트 변수를 확인하고 myMethod
예제에있는 변수로 바꿉니다 .
return $this->_response($this->myMethod($this->args));
보시다시피, 이제 클래스에있는 메서드를 호출하고 있습니다. 존재하지 않는 것으로 엔드 포인트를 설정하면 오류가 발생합니다.
myMethod가 my name is bob
다음 과 같은 문자열을 반환 $this->myMethod($this->args)
하면 PHP가 실행 되면 해당 값을 인수로 확인하여 다음 과 같은 $this->_response()
결과 를 얻습니다.
return $this->_response('my name is bob');
해당 이벤트 체인에 따라 processAPI()
메서드는 마지막으로 메서드가 수행하는대로 인코딩 된 JSON 문자열을 반환합니다 _response
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다