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 :
InstallationCreate 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
- $app->run();
- to
- $app->run($app->make('request'));
Don't forget to uncomment following lines in following path bootstrap/app.php
- $app->withFacades();
- $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.
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=demo
DB_USERNAME=root
DB_PASSWORD=your_password
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.
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- class Product extends Model
- {
- public $fillable = ['name','details'];
- }
In this step, we will add some routes to handle request.
app/Http/routes.php
- $app->get('product','ProductController@index');
- $app->get('product/{id}','ProductController@getProduct');
- $app->post('product','ProductController@createProduct');
- $app->post('product/{id}','ProductController@updateProduct');
- $app->delete('product/{id}','ProductController@deleteProduct');
In this step, we will create ProductController.php in following path app/Http/Controllers
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use App\Product;
- class ProductController extends Controller
- {
- public function index(Request $request)
- {
- $products = Product::all();
- return response()->json($products);
- }
- public function getProduct($id){
- $product = Product::find($id);
- return response()->json($product);
- }
- public function createProduct(Request $request)
- {
- $product=Product::create($request->all());
- return response()->json($product);
- }
- public function updateProduct(Request $request, $id)
- {
- $product=Product::find($id);
- $product->name = $request->input('name');
- $product->details = $request->input('details');
- $product->save();
- return response()->json($product);
- }
- public function deleteProduct($id){
- $product = Product::find($id);
- $product->delete();
- return response()->json('deleted');
- }
- }
- ?>
Now you can use this code for creating RESTful API in Lumen.