PHP code example of marko / api

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

    

marko / api example snippets


use Marko\Api\Resource\JsonResource;

class PostResource extends JsonResource
{
    public function toArray(): array
    {
        return [
            'title'  => $this->resource->title,
            'slug'   => $this->resource->slug,
            'body'   => $this->resource->body,
            'author' => $this->resource->author,
        ];
    }
}

// In a controller:
return new PostResource($post)->toResponse();
// => {"data": {"title": "Hello World", ...}}