Skip to content

Commit 2d615cb

Browse files
committed
Clean Code, add get,post and put functionality
1 parent f44e31c commit 2d615cb

File tree

5 files changed

+79
-48
lines changed

5 files changed

+79
-48
lines changed

api/items.php

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
header("Access-Control-Allow-Origin: *");
3+
header("Content-Type: application/json; charset=utf-8");
4+
$sql = new Sql();
25
$query = "SELECT * FROM items";
36
$items = $sql->getItems($query);
47
exit(json_encode($items));

index.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
session_start();
3-
require_once "lib/sql.php";
4-
require_once "lib/helper.php";
3+
require_once "lib/Sql.php";
4+
require_once "lib/Helper.php";
55

6-
route("/","api/items");
6+
get("api/items","items");
77
?>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<?php
2-
//rename to database.php
3-
$db = array(
4-
5-
"default" => array(
2+
$db = [
3+
"default" => [
64
"server" => "localhost",
75
"user" => "root",
86
"pass" => "",
9-
"dbname" => "db",
7+
"dbname" => "pos",
108
"driver" => "mysql"
11-
)
12-
);
9+
]
10+
];

lib/helper.php

+64-34
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,77 @@
11
<?php
2-
//get uri
3-
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
4-
"://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
5-
$path = parse_url($url, PHP_URL_PATH);
6-
//remove string from last "/" to first character
7-
$path = trim(ltrim(strchr($path,"/"),"/"));
8-
$path = ltrim(substr($path,strposX($path,"/",1)),"/");
9-
10-
$sql = new Sql();
11-
12-
function strposX($haystack, $needle, $number = 0)
13-
{
14-
return strpos($haystack, $needle,
15-
$number > 1 ?
16-
strposX($haystack, $needle, $number - 1) + strlen($needle) : 0
17-
);
18-
}
2+
$path = $_SERVER['REQUEST_URI'];
3+
//get $path except first "/"
4+
$path = substr($path, 1);
5+
//remove string from start to second "/"
6+
$path = substr($path, strpos($path, "/") + 1);
7+
8+
$method = $_SERVER['REQUEST_METHOD'];
199

20-
function api($api=null){
10+
function api($routeName,$api,$method){
2111
global $path;
22-
if($api!=null) $path = $api;
23-
if(isset($_POST)) extract($_POST);
24-
if(isset($_GET)) extract($_GET);
12+
$route = $routeName;
13+
//for request with parameters in uri
14+
if(strpos($routeName,"{")){
15+
//get routeName without parameters
16+
$tempRoute = substr($routeName,0,strpos($routeName,"{")+1);
17+
$route = substr($tempRoute,0,strlen($tempRoute)-1);
18+
//get param keys
19+
$paramKeys = substr($routeName,strpos($routeName,"{")+1);
20+
$paramKeys = substr($paramKeys,0,strlen($paramKeys)-1);
21+
$paramKeys = explode(",",$paramKeys);
22+
23+
24+
//check if routename exists in request uri
25+
if(str_contains($path,$route)){
26+
$pos = strrpos($route,"/");
27+
$paramValues = substr($path,$pos+1);
28+
$paramValues = explode("/",$paramValues);
29+
if(count($paramKeys) != count($paramValues))
30+
notFound();
31+
$params = array_combine($paramKeys,$paramValues);
32+
$path = substr($path,0,strlen($route)-1);
33+
$route = substr($tempRoute,0,strlen($tempRoute)-2);
34+
extract($params);
35+
}
36+
}
37+
if($route != $path)
38+
return;
39+
$path = $api;
40+
extract($method);
2541
if(!file_exists("api/$path.php")){
2642
if(!file_exists("api/$path/index.php")){
27-
header("HTTP/1.1 404 Not Found");
28-
exit("URL not found");
43+
notFound();
2944
}
3045
$path = $path."/index";
3146
}
32-
global $sql;
33-
header('Cache-Control: max-age=86400');
34-
header("Access-Control-Allow-Origin: *");
35-
header("Content-Type: application/json; charset=utf-8");
47+
3648
include "api/$path.php";
49+
exit();
3750
}
3851

39-
function route($routeName,$api){
40-
global $path;
41-
if($routeName==$path){
42-
api($api);
43-
exit();
44-
}
45-
}
52+
function post($routeName,$api){
53+
global $method;
54+
if($method != "POST") return;
55+
api($routeName,$api,$_POST);
56+
}
57+
58+
function get($routeName,$api){
59+
global $method;
60+
if($method != "GET") return;
61+
api($routeName,$api,$_GET);
62+
}
63+
64+
function put($routeName,$api){
65+
global $method;
66+
if($method != "PUT") return;
67+
$_PUT = json_decode(file_get_contents('php://input'),true);
68+
api($routeName,$api,$_PUT);
69+
}
70+
71+
72+
function notFound(){
73+
header("HTTP/1.1 404 Not Found");
74+
exit("URL not found");
75+
}
4676

4777

lib/sql.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Sql{
2020
* @author Van
2121
*/
2222
public function __construct($dbase="default",$server="",$user="",$pass="",$dbname="",$driver=""){
23-
require "database.php";
23+
require "Database.php";
2424
if($dbase != null){
2525
$server = $db[$dbase]["server"];
2626
$user = $db[$dbase]["user"];
@@ -30,9 +30,9 @@ public function __construct($dbase="default",$server="",$user="",$pass="",$dbnam
3030
}
3131
try{
3232
$this->conn = new PDO("$driver:host=$server;dbname=$dbname;",$user,$pass,
33-
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
33+
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
3434
}catch(PDOException $e){
35-
$this->error = $e->getMessage();
35+
exit($this->error = $e->getMessage());
3636
}
3737
}
3838

@@ -81,7 +81,7 @@ public function getItems($query,$inputs=null){
8181
try{
8282
$stmt = $this->conn->prepare($query);
8383
$stmt->execute($inputs);
84-
return $stmt->fetchAll(PDO::FETCH_ASSOC);
84+
return $stmt->fetchAll(PDO::FETCH_OBJ);
8585
}catch(PDOException $e){
8686
$this->error = $e->getMessage();
8787
return false;

0 commit comments

Comments
 (0)