Building RESTful API in Lumen, A Laravel Micro Framework

Building RESTful API in Lumen, A Laravel Micro Framework

Building RESTful API in Lumen, A Laravel Micro Framework

In this tutorial, I will tell you how to create simple RESTful API in Lumen.

In this example, we will store product info, list all products, update products and delete products.

But before going with example, i will let you know about Lumen and its features.

Lumen is a smaller, faster and leaner version of a full web application framework, in one word you can say Lumen is a “micro-framework” created by Taylor Otwell.

In PHP, there are two other popular micro-frameworks, Slim and Silex.

Lumen functionality is almost same as Laravel with some changes.

Lumen is designed for small app basically as you can use Lumen for RESTful API.

Lets start to creating a simple RESTful API in Lumen :

Create Project via Composer

In first step, install lumen project by running following command in your terminal:

composer create-project --prefer-dist laravel/lumen blog

After installing lumen, you can run this on your local server via localhost and see if it is working fine if getting any error like this "NotFoundHttpException in RoutesRequests.php" then change in public/index.php

  1. $app->run();
  2. to
  3. $app->run($app->make('request'));

Don't forget to uncomment following lines in following path bootstrap/app.php

  1. $app->withFacades();
  2. $app->withEloquent();

After fresh Lumen installation, you will see .env.example file at root directory. you will have to rename this file to .env and change database credential.


Create Model of Product Table

In this step, we will create a model for product table if you don't have product table then create a products table in your database with column 'name' and 'details' to run this demo code.

  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class Product extends Model
  4. {
  5. public $fillable = ['name','details'];
  6. }
Add Route

In this step, we will add some routes to handle request.

  1. $app->get('product','ProductController@index');
  2. $app->get('product/{id}','ProductController@getProduct');
  3. $app->post('product','ProductController@createProduct');
  4. $app->post('product/{id}','ProductController@updateProduct');
  5. $app->delete('product/{id}','ProductController@deleteProduct');
Create Product Controller

In this step, we will create ProductController.php in following path app/Http/Controllers

  1. namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use App\Http\Controllers\Controller;
  4. use App\Product;
  5. class ProductController extends Controller
  6. {
  7. public function index(Request $request)
  8. {
  9. $products = Product::all();
  10. return response()->json($products);
  11. }
  12. public function getProduct($id){
  13. $product = Product::find($id);
  14. return response()->json($product);
  15. }
  16. public function createProduct(Request $request)
  17. {
  18. $product=Product::create($request->all());
  19. return response()->json($product);
  20. }
  21. public function updateProduct(Request $request, $id)
  22. {
  23. $product=Product::find($id);
  24. $product->name = $request->input('name');
  25. $product->details = $request->input('details');
  26. $product->save();
  27. return response()->json($product);
  28. }
  29. public function deleteProduct($id){
  30. $product = Product::find($id);
  31. $product->delete();
  32. return response()->json('deleted');
  33. }
  34. }
  35. ?>

Now you can use this code for creating RESTful API in Lumen.

Phone: (+91) 8800417876
Noida, 201301