A simple RESTful Blog API built with Laravel 13. This project provides authentication, authorization, and CRUD operations for blog posts using Laravel Sanctum.
- User registration
- User login & logout using Laravel Sanctum
- Create, update, delete, and view blog posts
- Authorization using Laravel Policies
- Image upload for posts
- Search posts by title
- Pagination
- Form Request validation
- API Resources for consistent JSON responses
- Dockerized development environment
- Laravel 13
- PHP 8.3
- MySQL 8
- Laravel Sanctum
- Docker & Docker Compose
- Nginx
Clone the repository:
git clone <repository-url>
cd blog-apiCopy the environment file:
cp .env.example .envStart the containers:
docker compose up -d --buildInstall dependencies:
docker compose exec app composer installGenerate the application key:
docker compose exec app php artisan key:generateRun database migrations:
docker compose exec app php artisan migrateCreate the storage symlink:
docker compose exec app php artisan storage:linkThe API will be available at:
http://localhost:8000
Authentication is implemented using Laravel Sanctum.
After logging in or registering, use the returned access token in the Authorization header:
Authorization: Bearer YOUR_TOKEN
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/register |
Register a new user |
| POST | /api/login |
Login |
| POST | /api/logout |
Logout (Authenticated) |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/posts |
List all posts |
| GET | /api/posts/{post} |
Show a single post |
| POST | /api/posts |
Create a post |
| PUT | /api/posts/{post} |
Update a post |
| DELETE | /api/posts/{post} |
Delete a post |
Search posts by title:
GET /api/posts?search=laravel
- Any user can view posts.
- Only authenticated users can create posts.
- Only the author of a post can update or delete it.
Authorization is implemented using Laravel Policies.
app/
├── Http/
│ ├── Controllers/
│ │ └── Api/
│ ├── Requests/
│ └── Resources/
├── Models/
├── Policies/
└── ...
{
"message": "Post created successfully.",
"data": {
"id": 1,
"title": "Laravel 13",
"content": "Sample content"
}
}- Uploaded images are stored in
storage/app/public/posts. - Run
php artisan storage:linkbefore testing image uploads. - Pagination is enabled for the posts listing endpoint.
- Validation is handled using Laravel Form Requests.
- JSON responses are formatted using Laravel API Resources.
This project was created as part of a backend API coding challenge and is intended for demonstration purposes.