Laravel Introduction to REST APIs

What is an API?

  • API = Application Programming Interface.
  • It allows different software systems to communicate with each other.
  • Example: A mobile app calls a server’s API to get user data.

What is REST?

  • REST = Representational State Transfer.
  • It is an architectural style for designing APIs.
  • REST APIs use HTTP methods to perform actions on resources.
  • Resources are represented by URLs (endpoints).

📌 Example:

GET https://api.example.com/users

This fetches a list of users.


Key Principles of REST

  1. Stateless → Each request contains all info (server does not store session state).
  2. Client-Server Separation → Client (frontend) and server (backend) are independent.
  3. Uniform Interface → Same approach for accessing resources.
  4. Resource-Based → Everything (users, posts, products) is a resource, identified by a unique URL.
  5. Representation → Resources can be returned in JSON, XML, etc. (JSON is most common).



REST API HTTP Methods

Method

Purpose

Example

GET

Retrieve resource(s)

GET /users (get all users)

POST

Create new resource

POST /users (create new user)

PUT

Update entire resource

PUT /users/1 (update user with ID=1)

PATCH

Update partial resource

PATCH /users/1 (update only email of user 1)

DELETE

Remove resource

DELETE /users/1 (delete user 1)


API Resource Controllers

For API-only routes (no createedit):

php artisan make:controller ProductController --api

This will create app/Http/Controllers/ProductController.php. with index,store,show,update,destroy functions

class PostController extends Controller
{
    public function index(){   //    }

    public function store(Request $request){    //    }

    public function show(string $id){    //    }

    public function update(Request $request, string $id){   //    }

    public function destroy(string $id){    //    }
}

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.