Creating a REST API with Lumen

We will create a simple REST API for Add, Read, Update and Delete tasks for an article.
- GET /articles — Fetch all articles
- POST /articles Create a new article
- GET /article/{id} — Fetch a article by id
- PUT /article/{id} — Update a article by id
- DELETE /article/{id}- Delete a article by id
Step 1 create a lumen application with
lumen new blog
In the previous article, I have written the complete guide about lumen installation. If lumen is not installed in your system then please read this article
To serve our project locally run the following command:
php -S localhost:8000 -t public
Step 2: Configuration
Open .env file and replace the default CACHE_DRIVER and QUEUE_DRIVER values with the following:
CACHE_DRIVER=array QUEUE_DRIVER=database
Step 3: MySQL connection
I will use the MySQL database for this article for that please open the .env file and change the database values
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Step 4: enable the Eloquent and Facades
To use the Eloquent and Facades you need to uncomment the following lines in app\bootstrap.php
$app->withFacades();
$app->withEloquent();
Step 5: Database Migration
Create a table for Articels with id auto increment title,conent,pubsliher,publish_date. To create the table, run:
php artisan make:migration create_table_articles --create=articles
This will create a
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTableArticles extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->longText('description');
$table->string('publisher');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
Now run the following command to execute the migration.this will create the table and you can see the table in PHPMyAdmin
php artisan migrate
Step 6: The Model
Next, we will create a model file inside the app folder create an Article.php file and paste the below code
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title', 'description','pubsliher'
];
}
Step 7: Controller
Next, Create the articleController.php file inside the &lsqou; app/Http/Controller&rsqou; and paste the below code
<?php
namespace App\Http\Controllers;
use App\Article;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ArticleController extends Controller{
public function create(Request $request){
$article = Article::create($request->all());
return response()->json($article);
}
public function update(Request $request, $id){
$article = Article::find($id);
$article->title = $request->input('title');
$article->description = $request->input('description');
$article->publisher = $request->input('publisher');
$article->save();
return response()->json($article);
}
public function delete($id){
$article = Article::find($id);
$article->delete();
return response()->json('Removed successfully.');
}
public function show($id) {
$article = Article::find($id);
return response()->json($article);
}
public function index(){
$articles = Article::all();
return response()->json($articles);
}
}
?>
Step 8:Routes
Finally, we need to define our routes so let's do it by adding the route path in web.php file which is inside the routes folder. Open the file and paste the below code
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an routerlication.
| 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.
|
*/
$router->get('/', function () use ($router) {
return $router->app->version();
});
$router->group(['prefix'=>'api'], function() use($router){
$router->get('articles', 'ArticleController@index');
$router->post('article', 'ArticleController@create');
$router->put('article/{id}', 'ArticleController@update');
$router->get('article/{id}', 'ArticleController@show');
$router->delete('article/{id}', 'ArticleController@delete');
});
Now test the routes form postman. That's It. Thanks for reading the article. Hope this will help you to write first rest API using lumen