Api: Route action param can be Callable ([$obj, $method])

This commit is contained in:
Frank Schubert
2023-11-30 16:28:18 +01:00
parent fb2af39cbd
commit 1e7a5e12a4
3 changed files with 36 additions and 6 deletions

View File

@@ -0,0 +1,16 @@
<?php
class Preorder_Cif {
public function providerSetCif($code) {
return mfResponse::Ok(["message" => "in providerSetCif($code)"]);
}
public function getCifData() {
return mfResponse::Ok(["message" => "in getCifData()"]);
}
public function userSetCif() {
return mfResponse::Ok(["message" => "in userSetCif()"]);
}
}

View File

@@ -1,5 +1,7 @@
<?php
require_once(APPDIR."/Api/v1/Preorder/Preorder_Cif.php");
class PreorderApicontroller extends mfBaseApicontroller {
//private $filter_gemeinde_ids = [];
//private $campaign;
@@ -19,10 +21,17 @@ class PreorderApicontroller extends mfBaseApicontroller {
protected function init() {
$db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$this->addRoute("/preorder/open", "getOpenPreorders", "GET");
$cifObject = new Preorder_Cif();
$this->addRoute("/preorder", "submitPreorder", "POST");
$this->addRoute("/preorder/open", "getOpenPreorders", "GET");
$this->addRoute("/preorder/customerInstallationFeedback", [$cifObject, "getCifData"], "GET");
$this->addRoute("/preorder/customerInstallationFeedback", [$cifObject, "userSetCif"], "POST");
$this->addRoute("/preorder/:code", "getPreorder", "GET");
$this->addRoute("/preorder/:code", "cancelPreorder", "DELETE");
$this->addRoute("/preorder/:code/clientInstallationFinished", [$cifObject, "providerSetCif"], "GET");
$this->addRoute("/preorder/:code/serviceActivated", "setServiceActive", "POST");
$this->allowMissingOrigin = true;
}

View File

@@ -436,10 +436,9 @@ class mfBaseApicontroller {
}
//var_dump($params);exit;
$req_parts = explode("/", $params);
$req_count = count($req_parts);
foreach($this->routes as $route) {
if($route['method'] != $this->http_method) {
continue;
@@ -499,11 +498,17 @@ class mfBaseApicontroller {
}
private function call($function, $params = []) {
$args = $params;
if(count($params) === 1) {
return $this->__call($function,reset($params));
} else {
return $this->__call($function,$params);
$args = reset($params);
}
if(is_array($function)) {
return call_user_func($function, $args);
}
return $this->__call($function,$args);
}
protected function addRoute($route, $action, $method) {