Skip to content
Snippets Groups Projects
Commit b1c0848c authored by Sibidharan's avatar Sibidharan :speech_balloon:
Browse files

WIP: POST->GET bug

parent 0b0b6f58
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,8 @@ RewriteBase /
RewriteEngine On
RewriteRule ^/?api/([^/]+)?$ api/index.php?rquest=$1 [QSA,NC,L]
RewriteRule ^/?api/([^/]+)/([^/]+)?$ api/index.php?rquest=$2&namespace=$1 [QSA,NC,L]
## For general files, if not above, just remove .php
......
<?php
${basename(__FILE__, '.php')} = function(){
if($this->get_request_method() == "POST" and isset($this->_request['username']) and isset($this->_request['email']) and isset($this->_request['password'])){
$username = $this->_request['username'];
$email = $this->_request['email'];
$password = $this->_request['password'];
try{
$s = new Signup($username, $password, $email);
$data = [
"message" => "Signup success",
"userid" => $s->getInsertID()
];
$this->response($this->json($data), 200);
} catch(Exception $e) {
$data = [
"error" => $e->getMessage()
];
$this->response($this->json($data), 409);
}
} else {
$data = [
"error" => "Bad request",
"method" => $this->get_request_method(),
"server" => $_SERVER,
"post" => $_POST,
"get" => $_GET
];
$data = $this->json($data);
$this->response($data, 400);
}
};
\ No newline at end of file
......@@ -9,6 +9,7 @@ class API extends REST {
public $data = "";
private $db = NULL;
private $current_call;
public function __construct(){
parent::__construct(); // Init parent contructor
......@@ -22,14 +23,39 @@ class API extends REST {
*/
public function processApi(){
$func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
if((int)method_exists($this,$func) > 0)
$this->$func();
else
$this->response('',400); // If the method not exist with in this class, response would be "Page not found".
if((int)method_exists($this,$func) > 0){
$this->$func();
}
else {
if(isset($_GET['namespace'])){
$dir = $_SERVER['DOCUMENT_ROOT'].'/api/apis/'.$_GET['namespace'];
$methods = scandir($dir);
//var_dump($methods);
foreach($methods as $m){
if($m == "." or $m == ".."){
continue;
}
$basem = basename($m, '.php');
//echo "Trying to call $basem() for $func()\n";
if($basem == $func){
include $dir."/".$m;
$this->current_call = Closure::bind(${$basem}, $this, get_class());
$this->$basem();
}
}
} else {
//we can even process functions without namespace here.
$this->response($this->json(['error'=>'methood_not_found']),404);
}
}
}
public function __call($method, $args){
if(is_callable($this->current_call)){
return call_user_func_array($this->current_call, $args);
} else {
$this->response($this->json(['error'=>'methood_not_callable']),404);
}
}
/*************API SPACE START*******************/
......@@ -85,35 +111,6 @@ class API extends REST {
$this->response($data,200);
}
}
private function signup(){
if($this->get_request_method() == "POST" and isset($this->_request['username']) and isset($this->_request['email']) and isset($this->_request['password'])){
$username = $this->_request['username'];
$email = $this->_request['email'];
$password = $this->_request['password'];
try{
$s = new Signup($username, $password, $email);
$data = [
"message" => "Signup success",
"userid" => $s->getInsertID()
];
$this->response($this->json($data), 200);
} catch(Exception $e) {
$data = [
"error" => $e->getMessage()
];
$this->response($this->json($data), 409);
}
} else {
$data = [
"error" => "Bad request"
];
$data = $this->json($data);
$this->response($data, 400);
}
}
......
......@@ -20,10 +20,9 @@ class Superhero {
}
$dir = __DIR__.'/api/apis';
$methods = scandir($dir);
var_dump($methods);
foreach($methods as $m){
if($m == "." or $m == ".."){
echo $m;
continue;
}
$basem = basename($m, '.php');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment