laravel-rest-api
Building a RESTful API with Laravel 5.8
I recently created a simple RESTful API using Laravel 5.8, focusing on core features like user authentication and CRUD operations for posts and comments. The goal was to set up a clean and minimal backend that could serve as a starting point for future projects.
Features
- User registration and login with token-based authentication
- CRUD operations for posts and comments
- Use of Laravel API Resources for consistent JSON responses
- Protected routes for authenticated users
Getting Started
To get the project up and running:
-
Clone the repository:
git clone https://github.com/badass-commits/laravel-rest-api.git cd laravel-rest-api
-
Install dependencies:
composer install
-
Copy the example environment file and generate an application key:
cp .env.example .env php artisan key:generate
-
Configure your database settings in the
.env
file. -
Run migrations:
php artisan migrate
-
Start the development server:
php artisan serve
API Endpoints
POST /api/register
β Register a new userPOST /api/login
β Authenticate a user and receive a tokenGET /api/posts
β Retrieve all postsPOST /api/posts
β Create a new postGET /api/posts/{id}
β Retrieve a specific postPUT /api/posts/{id}
β Update a postDELETE /api/posts/{id}
β Delete a post
All routes under /api/posts
are protected and require authentication via a Bearer token.
Conclusion
This project serves as a foundational template for building RESTful APIs with Laravel 5.8. Itβs straightforward and easy to extend, making it suitable for small to medium-sized applications.
Feel free to check out the repository: laravel-rest-api