A simple PHP REST API for managing users, with a small vanilla JavaScript frontend.
- Custom PHP router (no framework)
- MySQL database with PDO
- Full CRUD operations: list, create, update, and delete users
- Interactive frontend with auto-loading user list
simple-user-api/
docker-compose.yml
README.md
app/
config/
Database.php
controllers/
UserController.php
core/
Router.php
models/
User.php
public/
index.php
frontend/
index.html
app.js
style.css
- PHP 8.0+
- PHP extension
pdo_mysql - Docker + Docker Compose (for local MySQL)
- Start MySQL:
docker compose up -d- Create the
userstable:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) NOT NULL UNIQUE,
username VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);- Optional seed data:
SOURCE database/seeds/users.sql;- Start the API server from project root:
php -S localhost:8000 -t public- Open the frontend:
- Open
frontend/index.htmlin your browser. - User list loads automatically on page load.
- Use the forms to create, update, or delete users.
http://localhost:8000
Returns all users.
Example response:
[
{
"id": 1,
"name": "Marco Paul",
"email": "marco@example.com",
"username": "marcop"
}
]Creates a user.
Request body:
{
"name": "John Doe",
"email": "john@example.com",
"username": "johnd",
"password": "secret123"
}Validation:
name,email,username, andpasswordare required.
Updates a user.
Request body:
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"username": "johnd",
"password": "secret123"
}Validation:
id,name,email,username, andpasswordare required.
Deletes a user.
Request body:
{
"id": 1
}Validation:
idis required.- Returns 404 if user not found.
Get all users:
curl http://localhost:8000/usersCreate user:
curl -X POST http://localhost:8000/users \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.com","username":"johnd","password":"secret123"}'Update user:
curl -X PUT http://localhost:8000/users \
-H "Content-Type: application/json" \
-d '{"id":1,"name":"John Updated","email":"john@example.com","username":"johnd","password":"secret123"}'Delete user:
curl -X DELETE http://localhost:8000/users \
-H "Content-Type: application/json" \
-d '{"id":1}'Current connection settings in app/config/Database.php:
- Host:
localhost - Database:
simple-user-api - Username:
root - Password:
secret
These values match docker-compose.yml defaults.
- Router uses exact URI matching from
$_SERVER['REQUEST_URI']. - CORS headers are configured in
public/index.php(supports GET, POST, PUT, DELETE). - Unknown routes return HTTP
404with messageRoute not found. - Frontend displays user ID in the list for easy reference when updating or deleting.