Pages

Monday 25 June 2018

Lumen Framework Installation (Laravel) for service

Here we are using lumen framework 5.0 (laravel) for service API and connecting with Drupal database.
Here we are using old version lumen 5.0. and install composer.

First command to run for install lumen-

composer create-project laravel/lumen assignment "5.0.*"

create .env file at root directory

write down this code for drupal database connection

APP_NAME=Lumen
APP_ENV=local
APP_DEBUG=true

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yourdatabasename
DB_USERNAME=yourusername
DB_PASSWORD=youruserpasword

if this error comes then

NotFoundHttpException in Application.php line 1093:

change line in public/index.php file-

$app->run();

Into:

$app->run($app->request);

/*Lumen is not trying to load the “.env” file in your project.
For this, open up “bootstrap/app.php”,| and un-comment the following line:
*/

Dotenv::load(__DIR__.'/../');

/* PHP Fatal error: Class ‘Schema’ not found in
|  “Schema” is a facade, and once again, facade is disabled by default.
| We need to enable it, by un-commenting the following line in the “bootstrap/app.php”.
*/

 $app->withFacades();

 $app->withEloquent();

If some storage permission issue then give permission 777 /storage folder

Now check with this url its shows "lumen." at your system. localhost/projectname/public/

Now create a route in directory/app/Http/routes.php

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

hit on browser localhost/projectname/public/hello its shows
"Hello World " then its working now.

and write in routes.php

$app->get('/hellodata', 'App\Http\Controllers\helloController@getAllData');


Now creates a file helloController.php under this directory-

/var/www/html/projectname/app/Http/Controllers/

<?php
namespace App\Http\Controllers;
use DB;

use Laravel\Lumen\Routing\Controller as BaseController;

class helloController extends BaseController {

public function getAllData() {
$results = DB::select("SELECT * FROM tablename");
print_r($results);
     
   }
}