Skip to content

Commit f50be80

Browse files
perf: composer setup
1 parent 64e8411 commit f50be80

22 files changed

+323
-216
lines changed

.gitignore

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1-
# Php Variables Files
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/vendor/
5+
composer.lock
6+
7+
# misc
8+
.DS_Store
9+
*.pem
10+
11+
# local env files
212
env.php

.htaccess

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
RewriteEngine on
22
RewriteRule ^([a-z]+)$ index.php?table=$1
3+
RewriteRule ^access_token=([a-z0-9]+)$ index.php?access_token=$1
34
RewriteRule ^([a-z]+)/access_token=([a-z0-9]+)$ index.php?table=$1&&access_token=$2
45
RewriteRule ^([a-z]+)/([0-9]+)$ index.php?table=$1&&id=$2
56
RewriteRule ^([a-z]+)/([0-9]+)/access_token=([a-z0-9]+)$ index.php?table=$1&&id=$2&&access_token=$3

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Php Rest API Application
22

3-
43
### Used Feature
54
> `json`, `headers_authorization`, `upload_file`, `access_token`
65
76
## Quick links
87
1. [**Get API**](http://localhost/php-rest-api/test/access_token=<Access_Token>) for `GET`, `POST`, `PUT`, `DELETE`
9-
2. [**Upload File**](http://localhost/php-rest-api/test/access_token=<Access_Token>) for `POST`, `DELETE`
8+
2. [**Upload File**](http://localhost/php-rest-api/file/access_token=<Access_Token>) for `POST`, `DELETE`
109

1110
### API Routes
1211
| HTTP Method | Path | Action | Scope | Desciption |

composer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "restjs/php-rest-api",
3+
"description": "Create Rest API Application",
4+
"type": "package",
5+
"keywords": [
6+
"php",
7+
"composer",
8+
"rest-api",
9+
"file-upload",
10+
"web-services",
11+
"jwt-authentication",
12+
"my-sql"
13+
],
14+
"homepage": "https://github.com/fullstackondemand/php-rest-api",
15+
"license": "MIT",
16+
"autoload": {
17+
"psr-4": {
18+
"RestJS\\PhpRestApi\\": "src/"
19+
}
20+
},
21+
"repositories": [
22+
{
23+
"type": "package",
24+
"package": {
25+
"name": "RestJS/php-rest-api",
26+
"version": "v1",
27+
"source": {
28+
"url": "https://github.com/fullstackondemand/php-rest-api.git",
29+
"type": "git",
30+
"reference": "master"
31+
}
32+
}
33+
}
34+
],
35+
"authors": [
36+
{
37+
"name": "Pramod Singh",
38+
"email": "spramodgusain@gmail.com",
39+
"homepage": "https://github.com/fullstackondemand",
40+
"role": "Developer"
41+
}
42+
],
43+
"minimum-stability": "dev",
44+
"prefer-stable": true,
45+
"require": {
46+
"php": "7.1.*|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*"
47+
}
48+
}

controllers/method.controller.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

index.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
<?php
22

3-
/* Include Varibles File */
4-
include 'env.php';
5-
6-
/* Include View Files */
7-
include 'views/header.view.php';
8-
include 'views/access_token.view.php';
9-
include 'views/table.view.php';
10-
11-
/* Check ID */
12-
if(!isset($_GET['id'])) { $id = null; }
13-
else { $id = $_GET['id']; }
3+
/* Use External Class */
4+
use RestJS\PhpRestApi\Rest;
145

15-
/* Check Upload File */
16-
if(isset($_GET['table']) && $_GET['table'] === "file") {
17-
include 'controllers/file.controller.php';
18-
$file = new FileController();
19-
$file->FileController($_SERVER['REQUEST_METHOD']);
20-
die();
21-
}
22-
23-
/* Method Controller File */
24-
include 'controllers/method.controller.php';
25-
$method = new MethodController();
26-
$method->methodController($_SERVER['REQUEST_METHOD'], $table , $id);
6+
/* Include Varibles File */
7+
require_once __DIR__ . '/vendor/autoload.php';
278

28-
?>
9+
/* Environment File Config */
10+
Rest::execute(__DIR__);

models/delete.model.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

controllers/file.controller.php renamed to src/controller/file.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3+
/* Define Namespace */
4+
namespace RestJS\PhpRestApi\Controller;
5+
36
/* File Controller Class */
4-
class FileController {
5-
6-
function __construct() { }
7+
class File {
78

89
/* Check Method Function */
9-
function fileController($method) {
10+
public static function fileUpload($dir, $method) {
11+
1012
switch ($method) {
1113

1214
/* POST Method */
@@ -35,15 +37,9 @@ function fileController($method) {
3537

3638
/* Upload File */
3739
if(move_uploaded_file($_FILES['upload']['tmp_name'], $url)){
38-
39-
/* Check HTTPS */
40-
if(isset($_SERVER['HTTPS'])){
41-
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
42-
}
43-
else{ $protocol = 'http'; }
4440

4541
/* Upload File Link */
46-
$url = $protocol."://".$_SERVER['SERVER_NAME'] ."/php-rest-api/".$url;
42+
$url = $dir ."/".$url;
4743

4844
echo json_encode(array('status'=>'Success', 'message'=>'File is successful uploaded.', 'file_url' => $url));
4945

@@ -55,15 +51,9 @@ function fileController($method) {
5551

5652
/* Recive Delete File URL */
5753
$data = json_decode(file_get_contents('php://input'), true);
58-
59-
/* Check HTTPS */
60-
if(isset($_SERVER['HTTPS'])){
61-
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
62-
}
63-
else{ $protocol = 'http'; }
6454

6555
/* Remove Host Link in URL */
66-
$url = str_replace($protocol."://".$_SERVER['SERVER_NAME'] ."/php-rest-api/", "",$data['upload']);
56+
$url = str_replace($dir ."/", "",$data['upload']);
6757

6858
/* Delete File */
6959
if(unlink($url)){
@@ -76,6 +66,4 @@ function fileController($method) {
7666

7767
}
7868
}
79-
}
80-
81-
?>
69+
}

src/controller/method.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/* Define Namespace */
4+
namespace RestJS\PhpRestApi\Controller;
5+
6+
/* Use External Class */
7+
use RestJS\PhpRestApi\Model\Delete;
8+
use RestJS\PhpRestApi\Model\Get;
9+
use RestJS\PhpRestApi\Model\Post;
10+
use RestJS\PhpRestApi\Model\Put;
11+
12+
/* Method Controller Class */
13+
class Method {
14+
15+
/* Check Method Function */
16+
public static function methodController($method, $table, $id) {
17+
18+
switch ($method) {
19+
20+
/* GET Method */
21+
case "GET":
22+
23+
/* Include GET Model Fuction */
24+
Get::getData($table, $id);
25+
break;
26+
27+
/* POST Method */
28+
case "POST":
29+
30+
/* Recive POST Data */
31+
$data = file_get_contents('php://input');
32+
33+
/* Include POST Model Fuction */
34+
Post::postData($table, $data);
35+
break;
36+
37+
/* PUT Method */
38+
case "PUT":
39+
40+
/* Recive PUT Data */
41+
$data = file_get_contents('php://input');
42+
43+
/* Include PUT Model Fuction */
44+
Put::putData($table, $id, $data);
45+
break;
46+
47+
/* DELETE Method */
48+
case "DELETE":
49+
50+
/* Include DELETE Model Fuction */
51+
Delete::deleteData($table, $id);
52+
break;
53+
}
54+
}
55+
}

src/model/delete.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/* Define Namespace */
4+
namespace RestJS\PhpRestApi\Model;
5+
6+
/* Use External Class */
7+
use RestJS\PhpRestApi\View\Database;
8+
9+
/* Delete Class */
10+
class Delete {
11+
12+
/* DELETE Data */
13+
public static function deleteData($table, $id) {
14+
15+
/* Check ID */
16+
if (!isset($id)) {
17+
echo json_encode(array('status' => 'Fail', 'error' => 'Please provide valid input.'));
18+
die();
19+
}
20+
21+
/* Include Database File */
22+
$con = Database::connection();
23+
24+
/* Delete Data */
25+
$sql = "DELETE FROM $table WHERE id= $id";
26+
27+
if ($con->query($sql) === TRUE) {
28+
echo json_encode(array('status' => 'Success', 'message' => 'Data is Deleted.'));
29+
}
30+
else {
31+
echo json_encode(array('status' => 'Fail', 'error' => 'Please provide valid input.'));
32+
die();
33+
}
34+
35+
}
36+
}

0 commit comments

Comments
 (0)