Create a basic Model

Before using Alambic we need to create a basic configuration to describe:

  • the data structure: Types
  • how data can be fetched: Connectors

Models can be constructed as PHP arrays or directly loaded from Json configuration files.

Create the following directories for storing config files

  • /schema
  • /schema/connectors
  • /schema/models

Setup connectors list

For this simple example, we will just use one of the built-in connectors: the Json connector, which map your types to json files. One key strength of Alambic is that a single API endpoint can handle multiple connectors.

Create the files.json file in /schema/connectors

Setup types

Types describe the objects than can be requested, and the relations between theses objects

We’ll first configure the Users model.

Create the users.json file in /schema/models

The following table describes the main properties of the users model:

Property Type Description
name String Model name
description String Model description
fields Array The user model exposes its native fields (“id” and “name”) and a new field “posts” as we would like to be able to retrieve a user along with his posts in one single request. Alambic can handle multiple types of relations between models (has one, has many, embeds one, embeds many, …) even through multiple datasources
singleEndpoint String The single endpoint “user” allows to request for one user at a time, based on the user id
multiEndpoint String The multi endpoint “users” allows to request a list of users
connector Object We will use the simple connector that we setup previously to fetch/write the users data in the users.json file

The Post model is pretty similar to the User one.

Create the posts.json file in /schema/models

Create an Alambic instance

Our alambic configuration is now complete, we just need to create a request endpoint to be able to request it.