PHP code example of vinelab / api-manager

1. Go to this page and download the library: Download vinelab/api-manager 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/ */

    

vinelab / api-manager example snippets


{
     "  "vinelab/api-manager": "*"
     }
 }

'providers' => array(
    ...
    'Vinelab\Api\ApiServiceProvider',

),

'mappers' => 'Lib\Api\Mappers\\',

'limit' => '50',

class PostMapper {

    public function map(Post $post)
    {
        return [
            'id'        => (int) $post->id,
            'title'     => $post->title,
            'body'      => $post->body,
            'published' => (boolean) $post->published
        ];
    }
}

$post = Post::create([
    'title'     => 'Some Title',
    'body'      => 'Things and spaces',
    'published' => true
]);

$mapper = new PostMapper;
return $mapper->map($post);

 namespace Lib\Api\Mappers;

class PostsMapper implements PostsInterface {

    public function map(array $data)
    {
        return [
            'id'     => (int) $data['id'],
            'title'  => $data['title'],
            'text'   => $data['text'],
            'active' => (boolean) $data['active']
        ];
    }

}

 namespace Lib\Api\Mappers;

class PostsMapper implements PostsInterface {

    public function map(Post $post)
    {
        return [
            'id'     => (int) $post->id,
            'title'  => $post->title,
            'text'   => $post->text,
            'active' => (boolean) $post->active
        ];
    }

}

Api::respond($mapper, $data, $total = null, $page = null, $status = 200, $headers = [], $options = 0)

return Api::respond('PostsMapper', Post::paginate(3));

return Api::respond('PostsMapper',Post::first());

$data = Post::where('active', '0')->get();
$total = $data->count();
$page = 2;

return Api::respond('PostsMapper', $data, $total, $page);

Api::error($exception, $code = 0, $status = 500, $headers = [], $options = 0);

try {
    throw WhateverCustomException('You deserve it, this is your fault.', 1001);
} catch (WhateverCustomException $e)
{
	return Api::error($e, $e->getCode(), 401);
}

return Api::error('Something is wrong!!!', 1000, 505);
bash
php artisan config:publish vinelab/api-manager