Example RESTFUll API in Laravel
This repository serves as a comprehensive example of a meticulously crafted RESTful API using Laravel. It not only showcases the implementation of RESTful endpoints but also embodies best practices in coding, leveraging Laravel's robust features.
- Implementation of RESTful API Endpoints: Demonstrates a well-defined structure and functionality of various API endpoints adhering to RESTful principles.
- Leveraging Laravel's Response and Request Handling: Utilizes Laravel's built-in functionalities effectively for handling responses, managing requests, and ensuring secure data flow.
- Exemplary Demonstration of Clean and Structured Code: Emphasizes clean, maintainable, and structured coding practices, serving as a guide for developers to create organized and scalable applications.
This example repository offers an insightful reference for implementing RESTful APIs in Laravel. Dive into the codebase to gain a deeper understanding of utilizing Laravel's features effectively and implementing industry-best practices.
Primarily, this source code repository serves as an educational resource, exemplifying how to architect and structure a robust RESTful API project using Laravel. It aims to aid developers in comprehending the implementation of Laravel features and adopting optimal coding practices for their projects.
Feel free to explore, learn, and adapt the showcased patterns and practices to enhance your own projects.
- PHP ^7.3 | ^8.0
- laravel 8
-
Clone the repository:
git clone https://github.com/amirsahra/todo-task-api.git
-
Install dependencies:
composer install
-
Set up environment variables by creating a
.env
file:cp .env.example .env
Update
.env
with your configuration (database, app key, etc.). -
Run migrations and seeders (if applicable):
php artisan migrate --seed
Explain how to use your API:
-
Start the development server:
php artisan serve
-
Use tools like Postman or curl to interact with the API endpoints.
-
Login
- Method:
POST
- Description: Logs in a user with their credentials.
- Example:
POST http://127.0.0.1:8000/api/V1/login Body: { "email": "vhammes@example.com", "password": "123456789" }
- Method:
-
Logout
- Method:
POST
- Description: Logs out the currently logged-in user.
- Example:
POST http://127.0.0.1:8000/api/V1/logout Body: { "email": "vhammes@example.com", "password": "123456789" }
- Method:
-
Register
- Method:
POST
- Description: Registers a new user.
- Example:
POST http://127.0.0.1:8000/api/V1/register Body: { "name": "amir", "email": "amirhosein.sahra@gmail.com", "password": "123456789" }
- Method:
-
Show Current User
- Method:
GET
- Description: Retrieves information about the currently logged-in user.
- Example:
GET http://127.0.0.1:8000/api/user Headers: { "Authorization": "Bearer 1|pY6W2ys7VWlTgaSr4pAbc0XOVIRWcbdNihqaWCMU", "Accept": "application/json", "Referer": "localhost" }
- Method:
-
Index
- Method:
GET
- Description: Retrieves all tasks.
- Example:
GET http://127.0.0.1:8000/api/V1/tasks Headers: { "Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2", "Accept": "application/json" }
- Method:
-
Show
- Method:
GET
- Description: Retrieves details of a specific task by ID.
- Example:
GET http://127.0.0.1:8000/api/V1/tasks/1 Headers: { "Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2", "Accept": "application/json" }
- Method:
-
Store
- Method:
POST
- Description: Creates a new task.
- Example:
POST http://127.0.0.1:8000/api/V1/tasks Headers: { "Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2", "Accept": "application/vnd.api+json" } Body: { "name": "amir hossein sahra" }
- Method:
-
Update
- Method:
PUT
- Description: Updates an existing task by ID.
- Example:
PUT http://127.0.0.1:8000/api/V1/tasks/13 Headers: { "Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2", "Accept": "application/json" } Body: { "name": "new name update" }
- Method:
-
Completed
- Method:
PATCH
- Description: Marks a task as completed by ID.
- Example:
PATCH http://127.0.0.1:8000/api/V1/tasks/10/complete Headers: { "Authorization": "Bearer 1|pY6W2ys7VWlTgaSr4pAbc0XOVIRWcbdNihqaWCMU", "Accept": "application/json" }
- Method:
-
Destroy
- Method:
DELETE
- Description: Deletes a task by ID.
- Example:
DELETE http://127.0.0.1:8000/api/V1/tasks/11 Headers: { "Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2" }
- Method:
This API utilizes Token-Based Authentication, specifically using the Bearer Token for authentication. In this method, a token serves as the authentication credential for each request sent.
To log in, a POST request is used at this address:
POST http://127.0.0.1:8000/api/V1/login
Body:
{
"email": "vhammes@example.com",
"password": "123456789"
}
This successful request provides the user with a token of type Bearer, which should be included in the headers of all subsequent requests:
Authorization: Bearer YOUR_ACCESS_TOKEN
For instance, in the headers of the following request, the token used for authentication is sent as a Bearer in the request:
GET http://127.0.0.1:8000/api/user
Headers:
{
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
"Accept": "application/json",
"Referer": "localhost"
}
This project is licensed under the MIT License - see the LICENSE file for details.