0%

Lumen from scratch

Lumen

ref: https://www.cloudways.com/blog/creating-rest-api-with-lumen/

Lumen (5.3.2) (Laravel Components 5.3.*)

Update

composer self-update
composer global update

Install

composer global require "laravel/lumen-installer"
lumen new car_api

.env

cp .env.example .env

APP_KEY: Vup31GMytLqiQHNjKOahXDbLsqiiFXbd

1
2
3
$app->get('/key', function() {
return str_random(32);
});

APP_TIMEZONE: UTC -> Asia/Taipei

DB_…

Enable Eloquent, Facades

bootstrap/app.php
uncomment

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

Migration

php artisan make:migration create_table_cars --create=cars

1
2
3
$table->string('make');
$table->string('model');
$table->string('year');

php artisan migrate

Model

app/Car.php

1
2
3
4
5
6
7
8
9
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Car extends Model
{
protected $fillable = ['make', 'model', 'year'];
}
?>

Controller

app/Http/Controllers/CarController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace App\Http\Controllers;
use App\Car;
use App\Http\Controllers\Controller;
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('Removed successfully.');
}
public function index(){
$cars = Car::all();
return response()->json($cars);
}
}
?>

Route

routes/web.php

1
2
3
4
5
6
7
8
9
10
$app->group(['prefix' => 'api/v1','namespace' => 'App\Http\Controllers'], function($app)
{
$app->post('car','CarController@createCar');
$app->put('car/{id}','CarController@updateCar');
$app->delete('car/{id}','CarController@deleteCar');
$app->get('car','CarController@index');
});

Testing!

Create

curl -i -X POST -H "Content-Type:application/json" http://car_api.dev/api/v1/car -d '{"make":"toyota", "model":"camry", "year":"2016"}'

View

curl http://car_api.dev/api/v1/car

Update

curl -X PUT -H "Content-Type:application/json" http://car_api.dev/api/v1/car/1 -d '{"make":"toyota", "model":"supra", "year":"1999"}'

Delete

curl -X DELETE http://car_api.dev/api/v1/car/1

Simple Secure

bootstrap/app.php
uncomment

1
2
3
4
5
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
......
$app->register(App\Providers\AuthServiceProvider::class);

app/Providers/AuthServiceProvider.php

1
2
3
4
5
6
7
8
9
10
public function boot()
{
$this->app['auth']->viaRequest('api', function ($request) {
$header = $request->header('api_token');
if ($header && $header == 'hello im api token') {
return new User();
}
return null; // 一定要null
});
}

app/Http/Controllers/CarController.php

1
2
3
4
5
6
7
8
public function __construct()
{
$this->middleware('auth', ['only' => [
'createCar',
'updateCar',
'deleteCar'
]]);
}

or in routes/web.php

1
`$app->get('car', ['middleware' => 'auth', 'uses' => 'CarController@index']);`

curl -H "api_token:hello im api token" http://car_api.dev/api/v1/car

curl -X PUT -H "api_token:hello im api token" -H "Content-Type:application/json" http://car_api.dev/api/v1/car/1 -d '{"year":"1999"}'