Skip to content

Repository files navigation

Blog API

A simple RESTful Blog API built with Laravel 13. This project provides authentication, authorization, and CRUD operations for blog posts using Laravel Sanctum.

Features

  • 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

Tech Stack

  • Laravel 13
  • PHP 8.3
  • MySQL 8
  • Laravel Sanctum
  • Docker & Docker Compose
  • Nginx

Installation

Clone the repository:

git clone <repository-url>
cd blog-api

Copy the environment file:

cp .env.example .env

Start the containers:

docker compose up -d --build

Install dependencies:

docker compose exec app composer install

Generate the application key:

docker compose exec app php artisan key:generate

Run database migrations:

docker compose exec app php artisan migrate

Create the storage symlink:

docker compose exec app php artisan storage:link

The API will be available at:

http://localhost:8000

Authentication

Authentication is implemented using Laravel Sanctum.

After logging in or registering, use the returned access token in the Authorization header:

Authorization: Bearer YOUR_TOKEN

API Endpoints

Authentication

Method Endpoint Description
POST /api/register Register a new user
POST /api/login Login
POST /api/logout Logout (Authenticated)

Posts

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

Search posts by title:

GET /api/posts?search=laravel

Authorization

  • 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.


Project Structure

app/
├── Http/
│   ├── Controllers/
│   │   └── Api/
│   ├── Requests/
│   └── Resources/
├── Models/
├── Policies/
└── ...

Example Response

{
    "message": "Post created successfully.",
    "data": {
        "id": 1,
        "title": "Laravel 13",
        "content": "Sample content"
    }
}

Notes

  • Uploaded images are stored in storage/app/public/posts.
  • Run php artisan storage:link before 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.

License

This project was created as part of a backend API coding challenge and is intended for demonstration purposes.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages