Lumen is new PHP framework which is developed by Taylor Otwell the same author of the famous Laravel framework. Lumen is a micro framework which is smaller, faster, easy for web based application. It has been designed to develop small web application & web service.

Before we jump to installation, few thing you will need to make sure your server meets the following requirements:

  • PHP >= 5.6.4
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension

Lumen utilizes Composer to manage its dependencies. So, before using Lumen, make sure you have Composer installed on your machine.

First, navigate to server directory via terminal on your machine:

cd /var/www/html

Now hit the following command in your terminal to create your first-lumen project:

sudo composer create-project laravel/lumen

Above command will create a folder with a lumen and install all files with dependency.

To check our fresh application is working or not, navigate to lumen directory and run:

php -S localhost:8000 -t public

If you are getting Lumen (5.4.6) (Laravel Components 5.4.*) text in http://localhost:8000/ window, means you have successfully installed Lumen framework on your machine.

Environment Configuration

Create a new database in your phpmyadmin for your new application. After creating your database open your .env file and update details with yours. Here is my sample of .env for your reference.

APP_ENV=local
APP_DEBUG=true
APP_KEY=
APP_TIMEZONE=UTC

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=
DB_DATABASE=lumen
DB_USERNAME=root
DB_PASSWORD=ln

CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=array

Enable Eloquent

To enable Eloquent we need to edit the file bootstrap/app.php and remove the comment on the following lines:

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

Note: The first line enables the support for Facades which simplifies the usage of some of the core classes of the framework.

Migration

Let’s create a database schema with migration. We will create movies table which stores movies information. Open your terminal and navigate to application root folder and hit below command:

php artisan make:migration create_movies_table

Above migration, the command will create a 2017_06_07_115628_create_movies_table.php file under “database/migrations”. Open that file in your favorite editor and replace up & down functions with below code:

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
    Schema::create('movies', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('title');
        $table->string('director');
        $table->timestamps();
    });
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
    Schema::drop('movies');
}

To create movies table in your database just hit below code in your terminal and it will generate a table to your database.

php artisan migrate

Model

Create a movies model inside app directory with app/Movies.php

<?php
    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Movies extends Model
    {
        protected $fillable = ['title', 'director'];
    }
?>

Controller

Create a movies controller inside app/Http/Controllers directory with MoviesController.php

<?php
    namespace App\Http\Controllers;

    use App\Movies;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;

    class MoviesController extends Controller
    {
        public function index()
        {
            $Movies = Movies::all();
            return response()->json($Movies);
        }
    }
?>

Routes

Create a route which will return all movies records so add below code in your web.php which is located inside routes directory.

<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$app->get('/', function () use ($app) {
    return $app->version();
});

$app->group(['prefix' => 'api/v1'], function ($app) {
    $app->get('movies', 'MoviesController@index');
});

Now when you call this http://localhost:8000/api/v1/movies URL on the browser you will get all the movies in JSON format.

Have something to share on the same? Tweet us on Twitter @letsnurture or drop a post on the official Facebook page of LetsNurture. Share your thoughts with us, we will be happy to have a discussion on the same.

Happy Coding 😉

Want to work with us? We're hiring!
  • Rajendra

    Nice article. It would be great if you also cover Laravel 5.4 API.

    🙂

  • Ajarudin Gunga

    Interesting Post..!!