Nasrul Hazim Bin Mohamad

How to Use Lumen in Laravel

Oct
10

You love the speed of Lumen but you want to have the features of Laravel? No problem, just include Lumen inside Laravel!

Below are the steps to include Lumen inside of the Laravel

Please ensure to create an empty Laravel and Lumen project because you need to copy some files from Lumen to Laravel

Here the working sample of the Lumen in Laravel – Lumen in Laravel

Live: Laravel 101

Aug
10

Assalamualaikum,

It’s been a while since the last time I post in my blog.

Now just to share a bit of Laravel, 101 on my Facebook Live.

Training: Web Application Development with Lumen(API)

Jan
30

Date: Saturday, 6th February 2016

Available Seats: 25

Fee: Student – RM 100, Public – RM 250

Location: Level 2, above Yamaha Motorsport Shop Lot

REGISTER HERE

Objectives

To learn how to develop an API with micro-framework Lumen

Preparation

Following items need to be install first before coming to the class:

  • Laragon
  • Git & GitHub Account
  • Sublime Text 3
  • Postman (www.getpostman.com)

Prerequisites

PHP & MySQL is compulsory. A good understanding of OOP is value added.

Audience

Software developers with PHP and MySQL background.

Syllabus

  • Installation
  • Routing
  • Controllers
  • Responses
  • Database
  • Migrations
  • Eloquent Queries
  • API Integration

 

 

Lumen: API Development – Episode II

Jan
02

In this episode, we will configure the environment of the API, and setup some fake data. For those not yet read the Episode I, you can read it here.

In general, we have these steps:

  1. Configure .env
  2. Create model
  3. Create migration script & do migration
  4. Create seed script & do DB Seeding

For the first few steps will straightforward before we continue with Eloquent.

  1. Rename .env.example to .env
  2. Generate 32 random characters and update the APP_KEY – I use textmechanic to generate random characters
  3. Next, open up bootstrap/app.php and uncomment the following lines:
    • Line 6 – to enable to load .env file
    • Line 25 – to enable Eloquent

Now we’re done with minimum requirement configuration for our API. Next, we going to setup our fake data.

Before proceed to the next section, if you noticed that I did not change much in .env. This is due we’re using homestead – everything is ready for our development, including the database. So, no need to further change the configuration file .env, unless we’re moving to production, you may want to configure for production.

For our fake data, I have a table called departments. The departments table have the id, name and ordering.

In this section, we will learn about Model, Migration and Seeds.

First, create a model for departments and save it in app directory.

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Department extends Model
{
	// for security concern, only allow these fields for mass assignments
	// Read more at https://laravel.com/docs/5.0/eloquent#mass-assignment
	protected $fillable = ['id','name','ordering'];

	// limit attributes displayed in model's array or JSON
	// https://laravel.com/docs/5.0/eloquent#converting-to-arrays-or-json
	protected $hidden = ['created_at','updated_at'];
}

Next, we need to create Migration scripts for departments table. Run the following in Git Bash.

php artisan make:migration create_departments_table --create=departments

And open up the database/migrations/[date-time]_create_departments_table.php and update the up method as following. This will preparing our departments table schema.

<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateDepartmentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('departments', function (Blueprint $table) { $table->increments('id');
            $table->string('name');
            $table->integer('ordering');
            $table->timestamps();
        });
    }

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

Now connect to homestead via vagrant ssh and then run the migration script

php artisan migrate

Next, we need to add some fake data using the faker. Open up database/factories/ModelFactory.php and paste in the following codes in it:

<?php $factory->define(App\Department::class, function ($faker) {
    return [
        'name' => $faker->name,
        'ordering' => mt_rand(1,50)
    ];
});

[Update 20/1/2016]
Next, to insert our data, open up database/seeds/DatabaseSeeders.php and add the following lines in method run.

<?php

use Illuminate\Database\Seeder;

// include the model
use App\Department;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
    	DB::statement('SET FOREIGN_KEY_CHECKS = 0');

    	// empty table first
    	Department::truncate();

    	// create 50 records
    	factory(Department::class, 50)->create();

        // $this->call('UserTableSeeder');
    }
}

Now enter the vagrant ssh and go to document root of the API, then run php artisan db:seed. You may verify your data by login to MySQL.

So, that’s done for preparing our data, using some fake data. Next episode will be talking about Route & Controller. Stay tuned for next episode!

Lumen: API Development – Episode I

Dec
30

So, this post is about develop your own API using Lumen, a micro framework, a child of the Laravel (I assumed it that way). This topic will take few parts and this is the Episode 1 of Lumen: API Development.

Following tools required to be install on your machine:

  1. Virtual Box – For those using Avira Anti Virus, you might need to head over this article if you having problem with Virtual Box.
  2. Vagrant – Enable developers to easily share the same environment configurations
  3. Composer – Enable developers to manage packages for system dependencies
  4. Git – Famous version control (please do create a GitHub account)
  5. Sublime Text 3 – best text editor I’ve used so far. 🙂

Here are the basic steps taken for setting up the Lumen(all commands running in Git Bash):

  1. Create Lumen project at directory lumen-api
    composer create-project laravel/lumen lumen-api
  2. Install homestead
    cd lumen-api
    composer require laravel/homestead
  3. Make homestead
    php vendor/bin/homestead make

    OR

    php vendor/laravel/homestead/homestead make
  4. Create id_rsa at /C/Users/[yourname]/.ssh
    ssh-keygen -t rsa -C yourname@homestead

    Location of the id_rsa

    Location of the id_rsa

  5. Open Homestead.yaml and change map folder to working directory (step #1)
    map: "D:/WWW/lumen-api"

    homestead.yaml Configuration

    homestead.yaml Configuration

  6. Update C:/Windows/System32/drivers/etc/hosts file
     192.168.10.10 lumen-api.com
  7. install vagrant laravel/homestead (size about 1GB..taking about 45 minutes on 4mbps connection.. T_T)
     vagrant box add laravel/homestead

    Vagrant Add Box Laravel/Homestead

    Vagrant Add Box Laravel/Homestead

  8. Open up Virtual Box & start up the vagrant
     vagrant up
  9. Try to SSH to homestead to check if it’s ok
     ssh vagrant@192.168.10.10 -p 22

    Try to SSH to Homestead

    Try to SSH to Homestead

  10. Browse to lumen-api.com to check if the homestead installed properly

    Test lumen-api.com

    Test lumen-api.com