What can we help you with?

If you are interested in finding out more about how we can help your organization, please provide your details and project requirements. We'll get back to you soon.

Micro framework of Laravel – Lumen

Laravel

A lightweight version of Laravel full-stack framework named as Lumen micro-framework, is introduced to strip down loading speed. Lumen is for tasks and parts that can profit by the ease and energy of Laravel, however, can bear to forfeit some configurability and adaptability in return for a speed support.

It is effortless to update to Laravel Development services anytime while working in Lumen.

Lumen

Installing Lumen

Install composer and change directory to server root folder.

command prompt

Create a lumen project “lumen_api” from “laravel/lumen” package (packagist.org).

create project

Composer will create a folder “lumen_api” and install all files of lumen including dependency.

Check all artisan command available in Lumen by

lumen api

Now, run Lumen on your local server and open localhost:8000 in your browser.

localhost

If below screen is visible, then Lumen is successfully installed.

localhost in browser

Configuration

Create a database in mysql and replace the credential “lumen_api /.env.example” file to “lumen_rest_ce/.env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=lumen_api
DB_USERNAME=root
DB_PASSWORD=your_password

Open “lumen_api/bootstrap/app.php” and uncomment following snippet

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

Migration

Create table “templates” containing total 4 columns, namely id (int & auto incremented), title ( varchar), createdby (varchar), created_at (timestamp) and updated_at (timestamp).

In terminal/command prompt write:

migration

It will create a migration file under “database/migration”.

Open the file, it will be seen that “CreateTempatesTable” class has been created. This class has two method one is “up” where table schema can be written and another is “down”, where table will be dropped which will be called at the time of rollback.

Now edit this migration file like this:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 class CreateTemplatesTable extends Migration {
    public function up()
    {
        Schema::create('templates', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('createdby');
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::drop('templates');
    }
}

To create the table in database, migrate or run migration file:
run migration file

Thereby, table will be created.

Model

Create a Template model under “app/Template.php” and use template table instance.

namespace App;  
use Illuminate\Database\Eloquent\Model;  
class Template extends Model
{   
    protected $fillable = ['title', 'createdby'];     
}

Even, create a TemplateController.php under “app/Http/Controllers”

namespace App\Http\Controllers;
 
use App\Template;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
 
 
class TemplateController extends Controller{
 
 .....
 .....
 
}

Route

Even mention some more routes and corresponding Controller method to create RESTful API.

Method URL Contoller@Method
GET http://localhost:8000/api/template TemplateController@index All Templates
GET http://localhost:8000/api/template/{id} TemplateController@getTemplate Fetch Template By id
POST http://localhost:8000/api/template TemplateController@createTemplate Create a template record
PUT http://localhost:8000/api/template/{id} TemplateController@updateTemplate Update Template By id
DELETE http://localhost:8000/api/template/{id} TemplateController@deleteTemplate Delete Template By id

Open “routes/web.php”, and add routes as below. It is required to run the APIs.

$router->get('/', function () use ($router) {
 return $router->app->version();
});
 
$router->get('api/template', ['as' => 'template', 'uses' => 'TemplateController@index']);
$router->get('api/template/{id}', ['as' => 'template', 'uses' => 'TemplateController@gettemplate']);
$router->post('api/template', ['as' => 'template', 'uses' => 'TemplateController@createTemplate']);
$router->put('api/template/{id}', ['as' => 'template', 'uses' => 'TemplateController@updateTemplate']);
$router->delete('api/template/{id}', ['as' => 'template', 'uses' => 'TemplateController@deleteTemplate']);

Controller

Edit TemplateController.php and according to routes implement those methods.

A demo for Modified TemplateController.php

namespace App\Http\Controllers; 
use App\Template; 
use App\Http\Controllers\Controller; 
use Illuminate\Http\Request; 
 
class TemplateController extends Controller{ public function index(){ $Templates = Template::all(); return response()->json($Templates);
 
    }
 
    public function getTemplate($id){
 
        $Template = Template::find($id);
 
        return response()->json($Template);
    }
 
    public function createTemplate(Request $request){
 
        $Template = Template::create($request->all());
 
        return response()->json($Template);
 
    }
 
    public function deleteTemplate ($id){
        $Template= Template::find($id);
        $Template->delete();
 
        return response()->json('deleted');
    }
 
    public function updateTemplate (Request $request,$id){
        $Template = Template::find($id);
        $Template->title = $request->input('title');
        $Template->author = $request->input('createdby');
        $Template->save();
 
        return response()->json($Template);
    }
 
}

Testing

By using chrome extension POSTMAN, APIs can be tested easily. User needs to provide API URL, Method, required parameters as input, to which POSTMAN will show API response as output.

testing

Wrapping Up

Lumen can be considered under Laravel Development services, which is particularly to strip down-speed of the site. It neither interrupts views and sessions nor other consumer-facing conveniences. It is just optimized for speedy microservices. Whether to use a full-stack structure like Laravel or a microframework like Lumen, it purely relies on programming necessities and Laravel developers ability and expertise.