A RESTful API for blog management built with PHP.
- RESTful API following PSR-12 coding standards
- Create, read, update, delete blog posts
- Filter posts by search terms
- JSON responses with proper HTTP status codes
- Layered architecture (MVC pattern)
- PDO database connection with prepared statements
- Composer autoloading (PSR-4)
- Proper error handling
blog-api-project/
├── config/
│ └── database.php # Database configuration
├── public/
│ ├── .htaccess # URL rewriting rules
│ └── index.php # Application entry point
├── src/
│ ├── Controllers/
│ │ └── PostController.php # Post controller
│ ├── Database/
│ │ └── DatabaseConnection.php # Database connection
│ ├── Exceptions/
│ │ └── ApiException.php # API exception handler
│ ├── Models/
│ │ └── Post.php # Post model
│ ├── Repositories/
│ │ └── PostRepository.php # Post repository
│ └── Services/
│ ├── Request.php # Request handler
│ ├── Response.php # Response handler
│ └── Router.php # Router
├── composer.json # Composer configuration
└── database.sql # Database schema
- Clone the repository
- Run
composer install - Create a MySQL database and import
database.sql - Configure database credentials in
config/database.php - Set up a virtual host pointing to the
publicdirectory (run cd public/ php -S localhost:8000)
GET /api/posts
GET /api/posts?search=keyword
GET /api/posts/{id}
POST /api/posts
Content-Type: application/json
{
"title": "Post Title",
"content": "Post content..."
}
PUT /api/posts/{id}
Content-Type: application/json
{
"title": "Updated Title",
"content": "Updated content..."
}
DELETE /api/posts/{id}
{
"success": true,
"data": {
"id": 1,
"title": "Post Title",
"content": "Post content...",
"created_at": "2025-11-04 12:00:00",
"updated_at": "2025-11-04 12:00:00"
}
}{
"success": false,
"message": "Error message",
"errors": {
"field": "Error description"
}
}200 OK: Successful request201 Created: Resource created204 No Content: Resource deleted400 Bad Request: Invalid request404 Not Found: Resource not found422 Unprocessable Entity: Validation error500 Internal Server Error: Server error
This project can be extended with:
- Authentication (JWT, OAuth)
- Pagination
- Rate limiting
- Caching
- Input validation middleware
- Database migrations
- Unit and integration tests
MIT