REST API using Lumen framework by Laravel
REST API using lumen framework by laravel
Installation Lumen via Composer :
install Composer documentation
|
1 |
composer create-project laravel/lumen --prefer-dist lumen-rest |
If the packages are downloaded then we will start to modify app.php file which is located atbootstrap/app.php for enable or disable the features what we need. So we are going to enable Eloquent and Facade.
bootstrap/app.php(Changes)
|
1 2 |
$app->withFacades(); $app->withEloquent(); |
Setting db using .env
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
APP_ENV=local APP_DEBUG=true APP_KEY=RandomKey DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=learn_lumen DB_USERNAME=root DB_PASSWORD= CACHE_DRIVER=memcached QUEUE_DRIVER=sync |
Create Migration :
|
1 |
php artisan migrate:install |
Create the migration table and the name table is articles
|
1 |
php artisan make:migration --create=articles create_articles_table |
After run, edit file in database/migrations
Code :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateArticlesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('articles', function (Blueprint $table) { $table->increments('id'); $table->String('article_name'); $table->text('article_description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('articles'); } } |
and then run migrate
|
1 |
php artisan migrate |
Create Model:
create article at app/ directory
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; /** * */ class Article extends Model { protected $table = 'articles'; protected $fillable = ['article_name','article_description']; } |
Create Controller:
create new ArticleController.php file to Http/Controller.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php namespace App\Http\Controllers; use App\Article; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class ArticleController extends Controller{ public function getArticles() { $articles = Article::all(); return response()->json($articles); } public function getArticle($id) { $article = Article::find($id); return response()->json($article); } public function saveArticle(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' => true ]); } public function updateArticle(Request $request, $id) { $article = Article::find($id); $article->article_name = $request->input('article_name'); $article->article_description = $request->input('article_description'); $article->save(); return response()->json($article); } } |
Setting Route :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It is a breeze. Simply tell Lumen the URIs it should respond to | and give it the Closure to call when that URI is requested. | */ // $app->get('/', function () use ($app) { // return $app->version(); // }); $app->group(['namespace' => 'App\Http\Controllers'] , function($app){ $api = 'api'; $app->get($api.'/', ['uses' => 'ArticleController@getArticles', 'as' => 'allArticles']); $app->get($api.'/article/{id}', ['uses' => 'ArticleController@getArticle', 'as' => 'singleArticle']); $app->post($api.'/article', ['uses' => 'ArticleController@saveArticle', 'as' => 'saveArticle']); $app->put($api.'/article/{id}', ['uses' => 'ArticleController@updateArticle', 'as' => 'updateArticle']); $app->delete($api.'/article/{id}', ['uses' => 'ArticleController@deleteArticle', 'as' => 'deleteArticle']); }); |
Done simple REST API implementation using lumen and you can check the api with Advanced REST client (Chrome), RESTClient (Mozilla Firefox).
Testing with Advanced REST client
Download project : https://github.com/MhdSyarif/lumen-rest-API
Referensi : wsnippets.com/create-rest-api-using-lumen-micro-framework-by-laravel
Dilihat sebanyak : 366 kali




























Leave a Reply