Skip to content

API Development

johnenrick edited this page Apr 28, 2017 · 1 revision

API Development

To create an API follow the steps below

  1. Create a migration for each table in database. The rull of thumb is one API for one database table.
  2. Create controller and model using php artisan. Note: controller name must have a Controller postfix
  3. Extend the created controller and model with APIController and APIModel
  4. Configure the controller. See API Controller Configuration
  5. Open routes/api_routes.php and add the new route
  6. Test the API by visiting localhost:8000/api/controller_name/operations. operations can be create, retrieve, update, delete

Configuration

API Controller Configuration

Adding a model

To add the API model for the controller, set $this->model = new Item(); where Item is the App/SampleModel

Adding a validation Rule

To add a validation rule, set the value of $this->validation in construct

$this->validation = [
  "description" => "required|unique:business_positions"
];

Specifying Column Default value

To specify default value, set the value of $this->defaultValue in construct. Default value only takes effect if required columns dont have value.

$this->defaultValue = [
  "description" => "Default value"
];

Adding Foreign Table

To add a foreign table, set the value of $this->requiredForeignTable in construct

$this->requiredForeignTable = [
  "sample_children_table"
];

Make Foreign Table Editable

By default, modifying foreign tables is not allowed. To bypass this, just set the value of $this->editableForeignTable in construct

$this->editableForeignTable = [
  "sample_children_table"
];

API Response

$response = array(
  "data" => null,
  "error" => array(),// {status, message}
  "debug" => null,
  "request_timestamp" => 0
);

Clone this wiki locally