Simple RESTfull API with Lumen – Laravel Microframework

Lumen is Laravel micro-framework that build for small web application and handle some micro-services. Nowadays people love to use Lumen for build fast API for their application.

Installing Lumen

if you have installed lumen installer using composer, you just need to type:

lumen new api

note

api : directory of project that you want to start build

or you can install lumen via composer create-project:

composer create-project laravel/lumen --prefer-dist api

Check all your artisan command by type:

php artisan

Run lumen on your local server

php artisan serve

 

Screen Shot 2015-12-29 at 9.12.45 PM

For full documentation you can read here.

when the package downloaded, you can start modify .env file from rename .env.example file. Fill the database detail setting to connect with your database in your local machine.

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomKey!!!
APP_LOCALE=en
APP_FALLBACK_LOCALE=en
DB_CONNECTION=mysql
DB_HOST=DATABASE_HOST
DB_DATABASE=DATABASE_NAME
DB_USERNAME=DATABASE_USER_NAME
DB_PASSWORD=DATABASE_PASSWORD

In the api/bootstrap/app.php uncommenting this following line:


Dotenv::load(__DIR__.'/../');
$app->withFacades();
$app->withEloquent();

 

Migration
this step is to create table in your database. In this example, there are article table that consist of 6 column.
id (auto increment), title varchar, content text, author varchar, update_at timestamp, and created_at timestamp.


php artisan make:migration --create=article create_article_table

 

it will create migration file under /database/migration . Edit xxx_create_article_table.php file

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticleTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('article', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->text('content');
            $table->string('author');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('article');
    }
}
save and run :

php artisan migrate

you are now success to create article table in your database.

 

Model
create article model under App/Article.php
<?php namespace App;
 
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
     protected $fillable = ['title', 'content', 'author'];
    
}
?>

 

Controller

create article controller under app/Http/Controllers/ArticleController.php

<?php
namespace App\Http\Controllers;
use App\Book;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ArticleController extends Controller{
    public function index(){
        $Articles  = Article::all();
        return response()->json($Articles);
    }
 
    public function getArticle($id){
        $Article  = Article::find($id);
        return response()->json($Article);
    }
 
    public function createArticle(Request $request){
        $Article =Article::create($request->all());
        return response()->json($Article);
 
    }
 
    public function deleteArticle($id){
        $Article  =Article::find($id);
        $Article->delete();
        return response()->json('success');
    }
 
    public function updateArticle(Request $request,$id){
        $Article  =Article::find($id);
        $Article->title = $request->input('title');
        $Article->content = $request->input('content');
        $Article->author = $request->input('author');
        $Article->save();
        return response()->json($Article);
    }
 
}

Routing
Add your routing under app/Http/routes.php


$app->get('/', function () use ($app) {
return $app->welcome();
});
$app->group(['prefix' => 'api/v1','namespace' => 'App\Http\Controllers'], function($app){
$app->get('article','ArticleController@index');
$app->get('article/{id}','ArticleController@getArticle');
$app->post('article','ArticleController@createArticle');
$app->put('article/{id}','ArticleController@updateArticle');
$app->delete('article/{id}','ArticleController@deleteArticle');
});

Run and test your api project on your local server using curl or i recomend you to use postman in chrome. Try in many method like get, post, put and delete.

example to get all article just type http://localhost/api/v1/article and set get method in your post man, it will return all article data in json format if you have already insert data before.

don’t forget to put id of article, if you want to request get 1 article, or update (put) and delete article ( single article )

if you want to add new article, simply you just need to fill the parameter title,content and author data that you want to add into database.

Advertisements

One thought on “Simple RESTfull API with Lumen – Laravel Microframework

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s