Skip to content

Commit 4fa4215

Browse files
committed
first commit
0 parents  commit 4fa4215

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2318
-0
lines changed

.env.example

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
APP_NAME="Raw PHP API"
2+
APP_ENV=local
3+
APP_DEBUG=true
4+
APP_URL=http://localhost:8000
5+
6+
DB_HOST=localhost
7+
DB_PORT=3306
8+
DB_DATABASE=test_db
9+
DB_USERNAME=root
10+
DB_PASSWORD=
11+
12+
JWT_SECRET=your-secret-key-here
13+
JWT_EXPIRE=3600
14+
15+
CACHE_DRIVER=file
16+
LOG_LEVEL=debug

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/vendor/
2+
/storage/logs/*.log
3+
/storage/cache/*
4+
/storage/sessions/*
5+
/storage/uploads/*
6+
.env
7+
composer.lock
8+
*.log
9+
.DS_Store
10+
Thumbs.db

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM php:8.1-apache
2+
3+
# Install PHP extensions
4+
RUN docker-php-ext-install pdo pdo_mysql
5+
6+
# Enable Apache mod_rewrite
7+
RUN a2enmod rewrite
8+
9+
# Copy application files
10+
COPY . /var/www/html/
11+
12+
# Set permissions
13+
RUN chown -R www-data:www-data /var/www/html/storage
14+
RUN chmod -R 755 /var/www/html/storage
15+
16+
# Set document root
17+
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
18+
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
19+
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
20+
21+
EXPOSE 80

README.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# PHP REST API Pro Kit
2+
3+
A production-ready Raw PHP REST API Starter Kit with JWT authentication, user management, file uploads, caching, rate limiting, and Docker support.
4+
5+
[![PHP Version](https://img.shields.io/badge/PHP-8.0%2B-blue.svg)](https://php.net)
6+
[![License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
7+
[![Docker](https://img.shields.io/badge/Docker-Ready-blue.svg)](docker-compose.yml)
8+
9+
## 🚀 Features
10+
11+
-**JWT Authentication** - Secure token-based authentication
12+
-**User Management** - Complete CRUD operations
13+
-**File Upload/Download** - Secure file handling
14+
-**Rate Limiting** - API request throttling
15+
-**Input Validation** - Request data validation
16+
-**Caching System** - File-based caching
17+
-**Error Handling** - Centralized error management
18+
-**Logging** - Request/error logging
19+
-**Health Checks** - System monitoring
20+
-**Docker Support** - Containerized deployment
21+
-**PHPUnit Testing** - Comprehensive test suite
22+
-**API Documentation** - Complete endpoint docs
23+
24+
## 📁 Project Structure
25+
26+
```
27+
├── app/
28+
│ ├── config/ # Configuration files
29+
│ ├── controllers/ # Request handlers
30+
│ ├── core/ # Core framework classes
31+
│ ├── database/ # Migrations and seeders
32+
│ ├── exceptions/ # Exception handlers
33+
│ ├── helpers/ # Utility classes
34+
│ ├── middleware/ # Request middleware
35+
│ ├── models/ # Data models
36+
│ ├── routes/ # Route definitions
37+
│ ├── services/ # Business logic
38+
│ └── tests/ # Test files
39+
├── bootstrap/ # Application bootstrap
40+
├── docs/ # API documentation
41+
├── public/ # Web server document root
42+
├── storage/ # Logs, cache, uploads
43+
└── vendor/ # Composer dependencies
44+
```
45+
46+
## ⚡ Quick Start
47+
48+
### 1. Clone Repository
49+
```bash
50+
git clone https://github.com/jmrashed/php-rest-api-pro-kit.git
51+
cd php-rest-api-pro-kit
52+
composer install
53+
```
54+
55+
### 2. Environment Setup
56+
```bash
57+
cp .env.example .env
58+
# Edit .env with your database credentials
59+
```
60+
61+
### 3. Database Setup
62+
```bash
63+
# Import the database schema
64+
mysql -u root -p < app/database/Database.sql
65+
```
66+
67+
### 4. Start Development Server
68+
```bash
69+
# PHP Built-in Server
70+
php -S localhost:8000 -t public
71+
72+
# Or with Docker
73+
docker-compose up -d
74+
```
75+
76+
### 5. Test the API
77+
```bash
78+
curl http://localhost:8000/api/health
79+
```
80+
81+
## 📚 API Endpoints
82+
83+
### Authentication
84+
- `POST /api/auth/register` - Register new user
85+
- `POST /api/auth/login` - Login user
86+
- `POST /api/auth/logout` - Logout user
87+
88+
### Users (Protected)
89+
- `GET /api/users` - Get all users (paginated)
90+
- `GET /api/users/{id}` - Get user by ID
91+
- `POST /api/users` - Create user
92+
- `PUT /api/users/{id}` - Update user
93+
- `DELETE /api/users/{id}` - Delete user
94+
95+
### Files (Protected)
96+
- `POST /api/files/upload` - Upload file
97+
- `DELETE /api/files/{id}` - Delete file
98+
99+
### System
100+
- `GET /api/health` - Health check
101+
- `GET /api/health/info` - System info
102+
103+
### Example Usage
104+
```bash
105+
# Register
106+
curl -X POST http://localhost:8000/api/auth/register \
107+
-H "Content-Type: application/json" \
108+
-d '{"name":"John Doe","email":"john@example.com","password":"password123"}'
109+
110+
# Login
111+
curl -X POST http://localhost:8000/api/auth/login \
112+
-H "Content-Type: application/json" \
113+
-d '{"email":"john@example.com","password":"password123"}'
114+
115+
# Get users (with token)
116+
curl -X GET http://localhost:8000/api/users \
117+
-H "Authorization: Bearer YOUR_JWT_TOKEN"
118+
```
119+
120+
## 🏗️ Architecture
121+
122+
### Core Components
123+
- **Application**: Main app bootstrap and container
124+
- **Router**: URL routing and middleware pipeline
125+
- **Request/Response**: HTTP abstraction layer
126+
- **Database**: PDO wrapper with query builder
127+
- **Authentication**: JWT-based auth system
128+
- **Cache**: File-based caching system
129+
- **Validation**: Input validation and sanitization
130+
131+
### Security Features
132+
- Password hashing (bcrypt)
133+
- JWT token authentication
134+
- Rate limiting (60 requests/hour per IP)
135+
- Input sanitization and validation
136+
- CORS middleware
137+
- SQL injection protection (prepared statements)
138+
139+
## 🧪 Testing
140+
141+
```bash
142+
# Run all tests
143+
vendor/bin/phpunit
144+
145+
# Run specific test
146+
vendor/bin/phpunit app/tests/Unit/UserTest.php
147+
```
148+
149+
## 🐳 Docker Deployment
150+
151+
```bash
152+
# Build and run
153+
docker-compose up -d
154+
155+
# View logs
156+
docker-compose logs -f app
157+
158+
# Stop services
159+
docker-compose down
160+
```
161+
162+
## 🛠️ Requirements
163+
164+
- PHP 8.0+
165+
- MySQL 5.7+
166+
- Composer
167+
- Docker (optional)
168+
169+
## 🤝 Contributing
170+
171+
1. Fork the repository
172+
2. Create feature branch (`git checkout -b feature/amazing-feature`)
173+
3. Commit changes (`git commit -m 'Add amazing feature'`)
174+
4. Push to branch (`git push origin feature/amazing-feature`)
175+
5. Open Pull Request
176+
177+
## 📄 License
178+
179+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
180+
181+
## 👨‍💻 Author
182+
183+
**Md Rashedul Islam**
184+
- GitHub: [@jmrashed](https://github.com/jmrashed)
185+
- LinkedIn: [Md Rashedul Islam](https://linkedin.com/in/jmrashed)
186+
187+
## ⭐ Show your support
188+
189+
Give a ⭐️ if this project helped you!

app/config/database.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
$servername = "localhost";
3+
$username = "root";
4+
$password = "";
5+
$database = "test_db";
6+
7+
$conn = new mysqli($servername, $username, $password, $database);
8+
9+
if ($conn->connect_error) {
10+
die("Connection failed: " . $conn->connect_error);
11+
}
12+
// echo "Connected successfully to test_db"; // Removed echo for API context

app/config/env.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Config;
4+
5+
class Env
6+
{
7+
protected static $variables = [];
8+
9+
public static function load($path)
10+
{
11+
if (!file_exists($path)) {
12+
throw new \Exception("Environment file not found at {$path}");
13+
}
14+
15+
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
16+
foreach ($lines as $line) {
17+
if (strpos(trim($line), '#') === 0) {
18+
continue;
19+
}
20+
21+
list($name, $value) = explode('=', $line, 2);
22+
self::$variables[trim($name)] = trim($value);
23+
}
24+
}
25+
26+
public static function get($key, $default = null)
27+
{
28+
return self::$variables[$key] ?? $default;
29+
}
30+
}

app/controllers/AuthController.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Core\Controller;
6+
use App\Core\Response;
7+
use App\Services\AuthService;
8+
9+
class AuthController extends Controller
10+
{
11+
private $authService;
12+
13+
public function __construct()
14+
{
15+
$this->authService = new AuthService();
16+
}
17+
18+
public function login()
19+
{
20+
$data = $this->getRequestData();
21+
22+
if (!isset($data['email']) || !isset($data['password'])) {
23+
return Response::json(['error' => 'Email and password required'], 400);
24+
}
25+
26+
$result = $this->authService->login($data['email'], $data['password']);
27+
28+
if ($result) {
29+
return Response::json($result);
30+
}
31+
32+
return Response::json(['error' => 'Invalid credentials'], 401);
33+
}
34+
35+
public function register()
36+
{
37+
$data = $this->getRequestData();
38+
39+
if (!isset($data['name']) || !isset($data['email']) || !isset($data['password'])) {
40+
return Response::json(['error' => 'Name, email and password required'], 400);
41+
}
42+
43+
$result = $this->authService->register($data);
44+
45+
if ($result) {
46+
return Response::json($result, 201);
47+
}
48+
49+
return Response::json(['error' => 'Registration failed'], 400);
50+
}
51+
52+
public function logout()
53+
{
54+
$token = $this->getBearerToken();
55+
56+
if ($token) {
57+
$this->authService->logout($token);
58+
}
59+
60+
return Response::json(['message' => 'Logged out successfully']);
61+
}
62+
}

0 commit comments

Comments
 (0)