Today we are going to build a REST API using Lumen which is a very popular microservice. When we develop want to develop only an API, we wont need default dependenices and libraries of Laravel. Therefore we'll use use Lumen.
Prerequisites
Lumen
To Continue with this tutorial, you should have installed Lumen in your pc. If you are still not installed Lumen in your machine you can configure it from here.
Postman
To test our API endpoints, you need a API Testing application like Postman. You can download Postman from here.
Setting up the Project
First you need to create a new lumen project by running below command in your terminal
Then navigate to your project directory by using below command in your terminal
1
cd rest-api
Setting up Database
To setup up Database for our project, open the application using your favourite text editor and and then navigate to .env file in it and then change below section acording to your Database settings:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->integer('price'); $table->longText('description'); $table->timestamps(); }); }
/** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } }
Then run php artisan migrate command to migrate our newly created product table migration.
Create Product Model
Now we need to create Model for our Product but by defualt Lumen not allow to create model using artisan command. So, go to your app directory and create new model called Product.php and paste below code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
namespace App; use Illuminate\Database\Eloquent\Model;
class Product extends Model { protected $table = 'products'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'price','description' ];
}
Note:
By default Lumen not load Eloquent and Facades.To use this we need to uncomment the following lines in bootstrap/app.php
namespace App\Http\Controllers; use App\Product; use Illuminate\Http\Request;
class ProductController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { // } public function index() { $products = Product::all(); return response()->json($products); }
public function create(Request $request) { $product = new Product; $product->name= $request->name; $product->price = $request->price; $product->description= $request->description; $product->save(); return response()->json($product); }
public function show($id) { $product = Product::find($id); return response()->json($product); }
To serve your project locally, you may use the Laravel Homestead virtual machine, Laravel Valet, or the built-in PHP development server:
1
php -S localhost:8000 -t public
Testing our API
Method
Route
Description
POST
/api/v1/items
Create an item
GET
/api/v1/items
Get All item
GET
/api/vi/items/:id
Get a single item
PUT
/api/v1/items/:id
Update item
DELETE
/api/v1/items/:id
Delete a item
Conclusion
In this tutorial, we implemented simple Lumen REST API. If you have any issue regarding this tutorial, mention your issue in the comment section or reach me through my E-mail. You can obtain complete source code for this tutorial from this GitHub repository.