Laravel Lumen is a stunningly fast PHP micro-framework for building web applications with expressive, elegant syntax. Lumen attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as routing, database abstraction, queueing, and caching.
The Lumen framework has a few system requirements:
You will define all of the routes for the application in the routes/api.php
file. The most basic Lumen routes simply accept a URI and a Closure:
$router->get('foo', function () {
return 'Hello World';
});
$router->post('foo', function () {
//
});
Here is an example of a basic controller class. All Lumen controllers should extend the base controller class:
<?php
namespace App\Http\Controllers;
use App\User;
class UserController extends Controller
{
/**
* Retrieve the user for the given ID.
*
* @param int $id
* @return Response
*/
public function show($id)
{
return User::findOrFail($id);
}
}
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request
class on your controller constructor or method. The current request instance will automatically be injected by the service container:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Store a new user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//
}
}
The json method will automatically set the Content-Type header to application/json
, as well as convert the given array into JSON using the json_encode
PHP function:
return response()->json(['name' => 'Abigail', 'state' => 'CA']);