Pt:4 Creating API end points with Laravel.
Introduction
Hello, we are beginning a new series on building API endpoints with laravel (REStful api), REST, which stands for relational state transfer, which is used for interaction from a frontend by just send data to the back-end or retrieving data from the backend through different transfer protocol.
Requirements
The following are the requirements for this tutorial.
- Download php 7.2.1
- Download Composer.exe
- Download xampp or wamp for windows.
- Download postman.
Difficulty level
This tutorial is rated as intermediate.
Recap of the previous tutorial.
In the previous series, we were taught how to create a service and register one, we also created some routes for basic CRUD for our application. The route i.e the api.php
was edited and the routes created. the basic protocols for initiation was explained, we also created the ArticcleController
and the various methods or function and its protocol of action was explained.
Overview of today's task.
In todays series, we would be creating the FetchAllArticleFeature, which is going to be served to the index method of the ArticleController. As i have said before, it uses the get protocol.
To start, we have to generate/create our feature.
Enter Lucid command prompt in vendor/bin
lucid make:feature FetchAllArticle Api
In the controller, ArticleController
grt the index function and add the code below.
public function index()
{
return $this->serve(FetchAllArticleFeature::class);
}
Add this use App\Services\Api\Features\FetchAllArticleFeature;
to include the file to the controller. In the index function, we are serving a FetchAllArticleFeature
to fetch all articles from the database, we are to create a job and a repository that interface with the model.
Creating the ArticleRepossitory.
Open up src/data/repositories
and add a file in the directory called ArticleRepository.php
in the newly created file add the snippet below.
<?php
namespace App\Data\Repositories;
use Framework\Article;
/**
* Base Repository.
*/
class ArticleRepository extends Repository
{
/**
* The model instance.
*
* @var \Illuminate\Database\Eloquent\Model
*/
public $model;
public function __construct()
{
$this->model = new Article;
parent::__construct($this->model);
}
}
The ArticleRepository
extends the base repository class, and the parent model is set to a new model called Article.
which is this line of code below
parent::__construct($this->model);
creating the FetchAllArticleJob
to create the job, we hit the lucid command line again.
lucid make:job FetchAllArticleJob Article
open up the job in src/domains/articles/jobs/FetchAllArticleJob.php
add the snippet below
<?php
namespace App\Domains\Article\Jobs;
use Lucid\Foundation\Job;
use App\Data\Repositories\ArticleRepository;
class FetchAllArticleJob extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle(ArticleRepository $article)
{
return $article->all();
}
}
in the above code of the handle method, we are returning all the entries from the repository (articles).
Creating the FetchAllArticleFeature
We generate our Feature with Lucid
lucid make:feature FetchAllArticle api
open the new created feature and add the snippet below
<?php
namespace App\Services\Api\Features;
use Lucid\Foundation\Feature;
use Illuminate\Http\Request;
use App\Domains\Article\Jobs\FetchAllArticleJob;
class FetchAllArticleFeature extends Feature
{
public function handle(Request $request)
{
$articles = $this->run(FetchAllArticleJob::class);
return $articles;
}
}
All we did here was just to use the job we created, and bomb its like magic, we returned all the articles.
Using Postman for our request.
Postman can help simulate the request.
use the GET protocol an send the request to the url `localhost:8000/api/vi/articles
and it would return json.
Conclusion
so we were able to return the articles in the database, in the next series, we would build the StoreArticleFeature
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
Violated Rule:
My Opinion:
It mainly shows ubiquitous functions and simple on-screen instructions.
A tutorial must be informative and explanatory, but also "tutor". This tutorial lacks "tutor"ing, and it is nothing more than "Do whatever I do." kind of tutorial.
We prefer tutorials as a whole as possible instead of dozens of parts. So, next time, please merge some topics together.
You can contact us on Discord.
[utopian-moderator]