2017 © Pedro Peláez
 

cakephp-plugin cakephp-api-resource

Api Resource for CakePHP

image

maymeow/cakephp-api-resource

Api Resource for CakePHP

  • Sunday, May 6, 2018
  • by maymeow
  • Repository
  • 1 Watchers
  • 0 Stars
  • 24 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 26 % Grown

The README.md

Cakephp Api Resource

Beerpay Build Status, (*1)

JSON API Resource plugin for CakePHP. This plugin is inspired with laravel's JSON API resources., (*2)

Requirements

  • CakePHP 3.6
  • PHP 7.1 or greater

Installation

Cakephp Api Resource plugin can be installed with Composer, (*3)

composer require maymeow/cakephp-api-resource

Usage

Creating resources

In this example ill show how to create UserResource. In your application create new file src/Http/Resources/UserResource.php., (*4)

UserResource.php will looks like this:, (*5)

<?php
namespace App\Http\Resources;

use MayMeow\API\Resource;

class UserResource extends Resource
{
    public function toArray()
    {
        return [
            'id' => $this->id,
            'email' => $this->email,
            'created_at' => $this->created
        ];
    }
}

Next you can use your newly created resource in your api controller. Example below:, (*6)

<?php
// ...
use App\Http\Resources\UserResource;
// ... class definition above
 public function index()
    {
        $query = $this->Users->find();

        $users = UserResource::collection($query);

        $this->set([
            'users' => $users,
            '_serialize' => ['users']
        ]);
    }

Single vs collection of resources

If you getting one instance of entity for example $this->Users->get($id) use:, (*7)

// BelongsTo, HasOne
$user = (new UserResource($userQuery))->get();

If you getting more instances for example index $allUsers = $this->Users->find() use:, (*8)

// HasMany, HasAndBelongsToMany
$users = UserResource::collection($query);

Anonymous functions

In cas you will need update properties before send them to client resources support anonymous functions. Following example show how to send html generated from markdown:, (*9)

in your resource file, (*10)

public function toArray()
    {
        return [
            'id' => $this->id,
            'raw_body' => $this->text,
            'html_body' => function ($q) {
                return (new Parsedown())->$text($q->text); // text is parsed before data is send to client
            }
        ];
    }

Associations beta

Resources can include each other., (*11)

public function toArray()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'profile' => function ($q) {
                return (new ProfileResource($q->profile))->get(); // single entity (belongsTo, HasOne)
            },
            'posts' => function ($q) {
                return PostResource::collection($q->posts); // collection of resources (hasMany)
            }
        ];
    }

Known bug: In beta do not include same association because in cause neverending loop., (*12)

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

History

SEE changelog, (*13)

Credits

  • MayMeow

License

MIT, (*14)

The Versions

06/05 2018

dev-master

9999999-dev

Api Resource for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by May Meow

plugin api cakephp resources

06/05 2018

v1.0

1.0.0.0

Api Resource for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by May Meow

plugin api cakephp resources