
Posted on • Edited on
Creating a RESTful API with Laravel
Introduction
In this tutorial, we will be creating a RESTful API using Laravel, a popular PHP web framework. A RESTful API allows for easy communication between client-side applications and servers. Laravel provides a robust set of tools to create and manage APIs quickly and easily.
Prerequisites
Before we begin, you should have the following installed:
You should also have a basic understanding of PHP and web development.
Steps
1). Create a new Laravel project
laravel new my-api
or
composer create-project laravel/laravel my-api
2). Create a new controller for our API endpoints
php artisan make:controller ApiController
3). Define our API routes inroutes/api.php
useApp\Http\Controllers\ApiController;Route::get('/users',[ApiController::class,'getUsers']);Route::get('/users/{id}',[ApiController::class,'getUserById']);Route::post('/users',[ApiController::class,'createUser']);Route::put('/users/{id}',[ApiController::class,'updateUser']);Route::delete('/users/{id}',[ApiController::class,'deleteUser']);
4). Define our API methods inapp/Http/Controllers/ApiController.php
namespaceApp\Http\Controllers;useIlluminate\Http\Request;useApp\Models\User;classApiControllerextendsController{publicfunctiongetUsers(){returnUser::all();}publicfunctiongetUserById($id){returnUser::find($id);}publicfunctioncreateUser(Request$request){$user=newUser;$user->name=$request->input('name');$user->email=$request->input('email');$user->password=$request->input('password');$user->save();return$user;}publicfunctionupdateUser(Request$request,$id){$user=User::find($id);$user->name=$request->input('name');$user->email=$request->input('email');$user->password=$request->input('password');$user->save();return$user;}publicfunctiondeleteUser($id){$user=User::find($id);$user->delete();returnresponse()->noContent();}}
5). Database configuration in .env file
DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=your_database_nameDB_USERNAME=your_database_user_nameDB_PASSWORD=your_database_password
6). To create the database, please open the terminal inside the project and run this command.
php artisan migrate
7). Test our API using a tool likePostman
Before testing, you'll need to serve the project.
php artisan serve
Once that's done, you can obtain the project server URL. Append "/api/" to the project link and use the resulting link to run the test.
- GET/users to retrieve all users
- GET/users/{id} to retrieve a specific user
- POST/users to create a new user
- PUT/users/{id} to update an existing user
- DELETE/users/{id} to delete a user
Conclusion
In this tutorial, we learned how to create a RESTful API with Laravel. We covered creating a new Laravel project, defining API routes and methods, and testing our API using a tool like Postman. Laravel provides a powerful and intuitive framework for creating and managing APIs, making it an excellent choice for web development projects.
Example project:-
restful_api By J-Sandaruwan
Thank you,
J-Sandaruwan.
linkedin
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse