Extending API with a New Entity in Venddor IO

In the Venddor IO environment, if you intend to enhance the API with a fresh module entity, a systematic approach is required. Here's a step-by-step guide to help you through this process:

Template for Your Entity Class:

namespace Tygh\Api\Entities;

use Tygh\Api\AEntity;
use Tygh\Api\Response;

class Things extends AEntity
{
    public function index($id = '', $params = array())
    {
        return array(
            'status' => Response::STATUS_OK,
            'data' => array()
        );
    }
    
    public function create($params)
    {
        return array(
            'status' => Response::STATUS_CREATED,
            'data' => array()
        );
    }
    
    public function update($id, $params)
    {
        return array(
            'status' => Response::STATUS_OK,
            'data' => array()
        );
    }
    
    public function delete($id)
    {
        return array(
            'status' => Response::STATUS_NO_CONTENT,
        );
    }
    
    public function privileges()
    {
        return array(
            'create' => 'create_things',
            'update' => 'edit_things',
            'delete' => 'delete_things',
            'index'  => 'view_things'
        );
    }
}

Note: If users attempt operations beyond their permissions, they will encounter a 403 (Forbidden) error.

Incorporating Extra Methods:

privilegesCustomer(): This method empowers you to specify the permissions for unauthorized users interfacing with this API entity. By default, these users cannot access the API.

To grant API access for unauthorized users, a slight modification in the config.local.php file, located in your store's root directory, is needed: Modify 'api_allow_customer' => false, to 'api_allow_customer' => true,.

public function privilegesCustomer()
{
    return array(
        'index' => true
    );
}

Both the privileges() and privilegesCustomer() methods give you the flexibility to employ true or false, as illustrated in the provided example.

By following this structure, you can seamlessly extend the capabilities of the Venddor IO platform, integrating new entities into its API, ensuring that your modules are both efficient and versatile.