# API Generator

Generate API with single command

### Use

Suppose we created module using php artisan make:crud Faq.

Now we need to generate API for that module

Well it can be done by using following command :-

```
php artisan make:api Faq
```

#### What it does

1. Creates client consummable API
2. Creates Restful API
3. Creates Resource(Resources/Faq/FaqResource) and CollectionResource(Resources/Faq/FaqCollection)
4. Creates ClientAPIController(Controller/Admin/API/Client/FaqClientAPIController) and RestAPIController(Controller/Admin/API/Restful/FaqRestAPIController)

### Client API

If you only want to generate client consummable API only

```
php artisan make:api Faq --client
```

#### ClientAPIController Example

```
//Controller/Admin/API/Client/FaqClientAPIController
<?php

namespace App\Http\Controllers\Admin\API\Client;

use App\Models\Admin\Faq;
use App\Http\Resources\Faq\FaqCollection;
use App\Http\Resources\Faq\FaqResource;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class FaqClientAPIController extends Controller
{
 
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return new FaqCollection(Faq::all());
    }

    /**
     * Display the specified resource.
     *
     * @param  App\Models\Admin\Faq  $faq
     * @return \Illuminate\Http\Response
     */
    public function show(Faq $faq)
    {
        return new FaqResource($faq);
    }
}

```

#### Generated API

In api.php

```
Route::group(['prefix' => 'client'], function () {
    Route::get('faq', [FaqClientAPIController::class, 'index']);
    Route::get('faq/{faq}', [FaqClientAPIController::class, 'show']);
});
```

![](https://3590348543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbCZnEbkx6hMOJRn5tS%2Fuploads%2FU4YcJiZbEdzOKWBYKOjE%2Fclientshowapi.jpg?alt=media\&token=f98a9e04-c3e9-4411-b6cc-b77c958e1080) ![](https://3590348543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbCZnEbkx6hMOJRn5tS%2Fuploads%2Fz19Tlxp3Ogema2KDQ6Gm%2Fclientshowallapi.jpg?alt=media\&token=0718cd25-3b1d-4060-ad87-dd12870437dd)

### Restful API

If you want to generate restful API only&#x20;

```
php artisan make:api Faq --client
```

#### RestAPIController Example

```
// Controller/Admin/API/Restful/FaqRestAPIController
<?php

namespace App\Http\Controllers\Admin\API\Restful;

use App\Http\Controllers\Controller;
use App\Models\Admin\Faq;
use App\Http\Requests\FaqRequest;
use App\Contracts\FaqRepositoryInterface;

class FaqRestAPIController extends Controller
{

    protected $faqRepositoryInterface;

    public function __construct(FaqRepositoryInterface $faqRepositoryInterface)
    {
        $this->faqRepositoryInterface = $faqRepositoryInterface;
        $this->authorizeResource(Faq::class, 'faq');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return response()->json($this->faqRepositoryInterface->indexFaq(), 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\FaqRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(FaqRequest $request)
    {
        $faq = $this->faqRepositoryInterface->storeFaq($request);
        return response()->json($faq, 200);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Admin\Faq  $faq
     * @return \Illuminate\Http\Response
     */
    public function show(Faq $faq)
    {
        return response()->json($faq, 200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\FaqRequest  $request
     * @param  \App\Models\Admin\Faq  $faq
     * @return \Illuminate\Http\Response
     */
    public function update(FaqRequest $request, Faq $faq)
    {
        $this->faqRepositoryInterface->updateFaq($request, $faq);
        return response()->json($faq, 200);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Admin\Faq  $faq
     * @return \Illuminate\Http\Response
     */
    public function destroy(Faq $faq)
    {
        $deleted_item = $faq;
        $faq->delete();
        return response()->json($deleted_item, 200);
    }
}

```

#### Generated API

In api.php

```
Route::group(['prefix' => 'rest', 'middleware' => ['auth:api']], function () {
    Route::resource('faq', FaqRestAPIController::class);
});
```

![](https://3590348543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbCZnEbkx6hMOJRn5tS%2Fuploads%2FU4YcJiZbEdzOKWBYKOjE%2Fclientshowapi.jpg?alt=media\&token=f98a9e04-c3e9-4411-b6cc-b77c958e1080) ![](https://3590348543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbCZnEbkx6hMOJRn5tS%2Fuploads%2Fz19Tlxp3Ogema2KDQ6Gm%2Fclientshowallapi.jpg?alt=media\&token=0718cd25-3b1d-4060-ad87-dd12870437dd)
