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 ArticleController
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
get the index function and add the code below, the code is responsible for serving the feature to the controller.
public function index()
{
return $this->serve(FetchAllArticleFeature::class);
}
Add this use App\Services\Api\Features\FetchAllArticleFeature;
to include the feature 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. this is done in lucid architecture where the controller is as thin as possible.
Creating the ArticleRepository.
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);
}
}
Code explanation
In the code above, we use the Articlerepository to extend the base repository and all its methods. in object oriented php, once there is a construct method, it is called before any other function.
in this case,
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();
}
}
code explanation
In the handle method, the ArticleRepository is injected here, it binds with Article model.
we can now query the model through the repository directly using the method on the base repository which was extended by ArticleRepository.
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
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @manishmike10, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @sirfreeman I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit