Creating A REST API With Lumen and MySQL – Part I

This REST API tutorial help to create restful API using PHP Lumen and MySQL.We will Create CRUD operation for customer module.

Well, Now everything is changing very rapidly and introducing a lot of plugin/cms/framework day by day, so here we will discussed micro rest framework sensation Lumen By Laravel, Yes its very popular rest framework to create rest API using PHP.

REST API With Lumen and MySQL

There are a lot of modules available for support multiple repetitive tasks alike, CORS etc. We will create rest API using Lumen and MySQL. I have added this tutorial into two parts, the first part will have lumen installation and configuration. Lumen configure will have database connection migrations script and the second part will have CRUD customer module functionality using RESTful API.

The Pre-requisites For Lumen Rest Api Application –

Lets move on, We need following pre-requisites software,

  • WAMP/XAMPP – Installed PHP and mysql
  • Composer

How To Install Lumen Using Composer

We can Installed Lumen latest rest framework using below command into root directory of /htdocs/.
composer create-project --prefer-dist laravel/lumen lumen_test

Above composer command will download latest lumen build into lumen_test folder, so the main lumen folder is /htdocs/lumen_test.This will download all dependency library as well into /vendor folder.

How To Configure MySQL With Lumen

MySQL is opensource database management system.You can easily integrate with any programming languages.We will add below parameters into .env file, if .env file does not exist, please create new one into root of the lumen project /htdocs/lumen_test/.env.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=abc123

The Parameters are,

  • DB_CONNECTION :
  • This will contain database driver information,like mysql,oracle.

  • DB_HOST :
  • This will contain hostname of database.

  • DB_DATABASE :
  • The name of database.

  • DB_USERNAME :
  • The username of MySQL database.

  • DB_PASSWORD :
  • The password of MySQL database.

How to create Migration script in Lumen/Laravel

We have configured database connection with MySQL. I will create a custom table using migration script. The migration is a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state.

Lumen are providing PHP artisan command for migration and seeding database.You can create migration using below command,
php artisan make:migration create_customer_table

Above command will create a PHP migration file into database\migrations folder, Now we will replace code of this file with below code,

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCustomerTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customer', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
			$table->string('address');
			$table->string('country');
			$table->integer('phone');
			$table->string('created_by');
			$table->string('updated_by');
            $table->timestamps();
        });
    }

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

As you can see, there is up and down method that will use for create table schema and drop the schema.The table name is 'customer'.We will run above migration script and create table into the database.

php artisan make:migration create_customer_table

Above command have been created 'customer' table into 'test' database.You can see there is another migration table has been created that will use for registering migration script.You can use rollback, reset, seed and refresh database table using php artisan command.