Building RESTful API in Lumen, A Laravel Micro Framework
In this post we will give you information about Building RESTful API in Lumen, A Laravel Micro Framework. Hear we will give you detail about Building RESTful API in Lumen, A Laravel Micro FrameworkAnd how to use it also give you demo for it if it is necessary.
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 :
Installation
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
- $app->run();
- to
- $app->run($app->make('request'));
$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();
$app->withFacades(); $app->withEloquent();
Configuration
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.
- <!--?php
- namespace App;
- use IlluminateDatabaseEloquentModel;
- class Product extends Model
- {
- public $fillable=['name','details'];
- }
<!--?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Product extends Model
{
public $fillable = ['name','details'];
}
Add Route
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');
$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');
Create Product Controller
In this step, we will create ProductController.php in following path app/Http/Controllers
- <!--?php
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- use AppHttpControllersController;
- use AppProduct;
- class ProductController extends Controller
- {
- public functionindex(Request $request)
- {
- $products= Product::all();
- returnresponse()->json($products);
- }
- public functiongetProduct($id){
- $product= Product::find($id);
- returnresponse()->json($product);
- }
- public functioncreateProduct(Request $request)
- {
- $product=Product::create($request->all());
- returnresponse()->json($product);
- }
- public functionupdateProduct(Request $request,$id)
- {
- $product=Product::find($id);
- $product->name =$request->input('name');
- $product->details =$request->input('details');
- $product->save();
- returnresponse()->json($product);
- }
- public functiondeleteProduct($id){
- $product= Product::find($id);
- $product->delete();
- returnresponse()->json('deleted');
- }
- }
- ?>
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.
Click here to download this project from github: GitHub
Hope this code and post will helped you for implement Building RESTful API in Lumen, A Laravel Micro Framework. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs