part 4 : Creating API endpoints in laravel

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@sirfreeman·
0.000 HBD
part 4 : Creating API endpoints in laravel
![articles.gif](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519637306/xnrwjjhbatibsbi06u0l.gif)

# 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`
![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519594087/pn1bsqlkocdwkci3nuis.png)

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, 
![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519621831/kaypqu4pd3xhxzcwbroe.png)

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`
![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519623021/o69yvxtsgl98yiprsbp3.png)


open up the job in `src/domains/articles/jobs/FetchAllArticleJob.php`
add the snippet below
``` php
<?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. 
![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519623350/tdkxoogpynhgee7qnn8i.png)

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;
    }
}
```
![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519623569/yhnxnyrtpmlpv0rtahup.png)

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.
![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519624041/rqfbk5dcnsfcgfe5tqy2.png)

# Conclusion
so we were able to return the articles in the database, in the next series, we would build the StoreArticleFeature

#### Curriculum
- [Pt:1](https://steemit.com/utopian-io/@sirfreeman/pt-1-creating-api-end-points-with-laravel)
- [Pt:2](https://steemit.com/utopian-io/@sirfreeman/pt-2-creating-api-end-points-with-laravel)
- [Pt:3](https://steemit.com/utopian-io/@sirfreeman/5q9frn-pt-2-creating-api-end-points-with-laravel)



<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@sirfreeman/part-4-creating-api-endpoints-in-laravel">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , ,