How To Create A REST API With Lumen

Submitted by olivedev - 2 months ago

Lumen is a micro-framework built on top of Laravel. The framework is ideal for small apps and services that are optimized for speed. The most important application of the framework is to build REST APIs. This tutorial explains how REST API can be created using Lumen. https://www.cloudways.com/blog/creating-rest-api-with-lumen/


<?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);
 
	}
}
?>
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);
 
	}
}
?>
comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks