Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Creating a RESTful API with Laravel
J-Sandaruwan
J-Sandaruwan

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
Enter fullscreen modeExit fullscreen mode

or

composer create-project laravel/laravel my-api
Enter fullscreen modeExit fullscreen mode

2). Create a new controller for our API endpoints

php artisan make:controller ApiController
Enter fullscreen modeExit fullscreen mode

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']);
Enter fullscreen modeExit fullscreen mode

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();}}
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

6). To create the database, please open the terminal inside the project and run this command.

php artisan migrate
Enter fullscreen modeExit fullscreen mode

7). Test our API using a tool likePostman
Before testing, you'll need to serve the project.

php artisan serve
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi, I’m @J-Sandaruwan (ජනිත් සඳරුවන්)
  • Location
    Hertfordshire
  • Joined

More fromJ-Sandaruwan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp