Overview
Lumen is a laravel-based micro-framework for small applications and microservices focused on performance and speed optimization, an important application of which is building restapi.
Why build rest APIs with lumen
Lumen accesses very fast the number of requests that can be processed per second is more than laravel using Nikic/fastroute instead of symphony, which improves performance
Installation & Configuration
About lumen detailed installation Tutorials Reference official documentation: Http://laravelacademy.org/post/6328.html
Here we use composer to install under the Web root directory:
Composer Create-project Laravel/lumen Rest_api
After the installation is complete, configure the database connection information in. ENV:
Db_database=<db_name>
Db_username=<db_username>
Db_password=<db_password>
Then uncomment the following two lines in bootstrap/app.php:
$app->withfacades ();
$app->witheloquent ();
At this point in the browser access Rest_api.dev (Mac use Valet,windows to set the virtual domain name), the page appears as follows:
Lumen (5.3.2) (Laravel components 5.3.*)
Database migration
Next we'll create the data table.
Run the following command in the project root directory:
PHP Artisan make:migration create_table_cars--create=cars
The command will create a migration file in the database/migrations/directory <date>_create_table_cars.php, and then we'll edit the file to define the data table.
Schema::create (' Cars ', function (Blueprint $table) {
$table->increments (' id ');
$table->string (' make ');
$table->string (' model ');
$table->string (' year ');
});
Now let's run this migration:
PHP Artisan Migrate
In this way, the corresponding table is created in the database:
Create a model
Next we create the model file car.php in the app directory and write the following code:
<?php namespace App;
Use Illuminate\database\eloquent\model;
Class Car extends Model
{
protected $fillable = [' Make ', ' model ', ' year '];
Public $timestamps = false;
}
Create a Controller
Then create the controller file app/http/controllers/carcontroller.php:
<?php
namespace App\http\controllers;
Use App\car;
Use Illuminate\http\request;
Class Carcontroller extends Controller
{
Public Function Createcar (Request $request)
{
$car = Car::create ($request->all ());
return response ()->json ($car);
}
Public Function Updatecar (Request $request, $id)
{
$car = Car::find ($id);
$car->make = $request->input (' make ');
$car->model = $request->input (' model ');
$car->year = $request->input (' year ');
$car->save ();
return response ()->json ($car);
}
Public Function Deletecar ($id)
{
$car = Car::find ($id);
$car->delete ();
return response ()->json (' delete succeeded ');
}
Public Function Index ()
{
$cars = Car::all ();
return response ()->json ($cars);
}
}
Defining routes
The rest is to configure the route, I will add and delete changes to configure the corresponding route. Open app/http/routes.php and add the following route:
$app->group ([' prefix ' = ' api/v1 '], function ($app)
{
$app->post (' car ', ' [email protected] ');
$app->put (' Car/{id} ', ' [email protected] ');
$app->delete (' Car/{id} ', ' [email protected] ');
$app->get (' car ', ' [email protected] ');
});
Note I put this set of routes under the API/V1 prefix.
Test API
Now let's test this set of rest APIs with curl.
Let's start by testing the creation:
Curl-i-X post-h "Content-type:application/json" http://rest_api.dev/api/v1/car-d ' {"Make": "Audi", "model": "TT", " Year ":" 2016 "} '
The output below indicates a successful creation:
http/1.0 OK
Host:rest_api.dev
Connection:close
x-powered-by:php/7.0.6
Cache-control:no-cache
Content-type:application/json
Date:sun, 07:06:13 GMT
{"Make": "Audi", "model": "TT", "Year": "", "id": 1}
Then we'll test the update of the record that we just created:
Curl-h "Content-type:application/json" HTTP://REST_API.DEV/API/V1/CAR/1-x put-d ' {"make": "BMW", "model": "X6", "year" : "2016"} '
The output is as follows, indicating that the update was successful:
{"id": 1, "make": "BMW", "model": "X6", "Year": "2016"}
Next we will test the list page :
Curl-h "Content-type:application/json" Http://rest_api.dev/api/v1/car-X GET
The output is as follows:
[{' id ': 1, ' make ': ' BMW ', ' model ': ' X6 ', ' Year ': ' 2016 '}]
Finally, we tested the removal API:
Curl-x DELETE HTTP://REST_API.DEV/API/V1/CAR/1
Process for building APIs with Lumen