PHP code example of isaackearl / artisan-api

1. Go to this page and download the library: Download isaackearl/artisan-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

isaackearl / artisan-api example snippets


// For Laravel add this to config/app.php
IsaacKenEarl\LaravelApi\Providers\ArtisanApiServiceProvider::class

// For Lumen add this to bootstrap/app.php 
$app->register(IsaacKenEarl\LaravelApi\Providers\ArtisanApiServiceProvider::class);


'Api' => IsaacKenEarl\LaravelApi\Facades\Api::class,

    private $api;

    public function __construct(ArtisanApiServiceInterface $apiService)
    {
        $this->api = $apiService;
    }

    public function index()
    {
        $users = User::all();
        return $this->api->respondWithCollection($users, new UserTransformer());
    }

// you can respondWithError or respondWithMessage and customize the status code 
// and response code etc
return $this->api
            ->setStatus(401)
            ->setResponseCode(ResponseCodes::UNAUTHORIZED)
            ->respondWithError('Not logged in');

class UserTransformer extends Transformer
{
    function transform($user)
    {

        return [
            'id' => $user->id,
            'name' => $user->name,
            'date_of_birth' => $user->date_of_birth->toDateString(),
            'email' => $user->getPrimaryEmail()
        ];
    }
}
 php
// do stuff like this
public function show() {
    return Api::respondWithItem($user, new UserTransformer());
}

// or like this:

return Api::respondNotFound();
bash
php artisan make:transformer UserTransformer
bash
php artisan vendor:publish --provider="Spatie\Fractal\FractalServiceProvider"