PHPackages                             adaiasmagdiel/rubik-rest - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. adaiasmagdiel/rubik-rest

ActiveLibrary[Database &amp; ORM](/categories/database)

adaiasmagdiel/rubik-rest
========================

Rest interface for Rubik ORM for integrations with Erlenmeyer

v1.1.1(4mo ago)3216gpl-3.0-onlyPHP

Since Dec 10Pushed 4mo agoCompare

[ Source](https://github.com/AdaiasMagdiel/rubik-rest)[ Packagist](https://packagist.org/packages/adaiasmagdiel/rubik-rest)[ RSS](/packages/adaiasmagdiel-rubik-rest/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (6)Used By (0)

RubikREST
=========

[](#rubikrest)

Rest interface for Rubik ORM designed for integrations with the Erlenmeyer framework.

RubikREST allows you to instantly expose your Rubik Models as a full-featured REST API with automatic routing, filtering, eager loading, and OpenAPI (Swagger) documentation. It includes a fluent JavaScript client for seamless frontend integration.

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- adaiasmagdiel/erlenmeyer ^5.0
- adaiasmagdiel/rubik ^6.0

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require adaiasmagdiel/rubik-rest
```

Quick Start
-----------

[](#quick-start)

### 1. Backend Setup

[](#1-backend-setup)

In your Erlenmeyer bootstrap file (e.g., `index.php`):

```
use AdaiasMagdiel\Erlenmeyer\App;
use AdaiasMagdiel\RubikREST\RubikREST;
use App\Models\User;
use App\Models\Post;

$app = new App();

// Configure and register resources
RubikREST::configure($app, '/api/v1')
    ->resource('users', User::class)
    ->resource('posts', Post::class, [AuthMiddleware::class]) // Protected route
    ->enableDocs('/api-docs'); // Enable Swagger UI

$app->run();
```

### 2. Security (Mass Assignment)

[](#2-security-mass-assignment)

By default, RubikREST operates in a permissive mode to allow rapid prototyping. For production environments, **you must define the `$fillable` property in your Rubik Models**.

If `$fillable` is defined, the API will strictly filter input data (POST/PATCH) to only allow the specified columns.

```
namespace App\Models;

use AdaiasMagdiel\Rubik\Model;

class User extends Model
{
    // Only these fields can be created or updated via the API
    public static array $fillable = ['name', 'email', 'bio'];
}
```

JavaScript Client
-----------------

[](#javascript-client)

This package includes `RubikREST.js`, a zero-dependency, fluent client for consuming the API.

```
import { RubikClient } from "./path/to/RubikREST.js";

const client = new RubikClient("https://api.example.com/v1");

// 1. Fetching data (List)
const { data, count, error } = await client
  .from("users")
  .select("id,name,email")
  .with("posts") // Eager load relationships
  .eq("role", "admin")
  .orderBy("created_at", "desc")
  .page(1, 20)
  .get();

// 2. Fetch single
const user = await client.from("users").find(1);

// 3. Create
await client
  .from("users")
  .create({ name: "John Doe", email: "john@example.com" });

// 4. Update
await client.from("users").update(1, { name: "Jane Doe" });

// 5. Delete
await client.from("users").delete(1);
```

API Features &amp; Query Parameters
-----------------------------------

[](#api-features--query-parameters)

The API supports a rich set of query parameters for the `GET /resource` endpoint.

### Selection &amp; Eager Loading

[](#selection--eager-loading)

- **select**: Comma-separated list of columns to return.
    - `GET /users?select=id,name`
- **with**: Comma-separated list of relationships to eager load (prevents N+1 problems).
    - `GET /users?with=posts,profile`

### Pagination &amp; Sorting

[](#pagination--sorting)

- **limit**: Number of records to return.
- **offset**: Number of records to skip.
- **order**: Format `column.direction`.
    - `GET /users?order=age.desc,name.asc`

### Filtering

[](#filtering)

Filters follow the format `column.operator=value`.

OperatorURL ExampleSQL Equivalent**eq**`status.eq=active``status = 'active'`**neq**`status.neq=banned``status  'banned'`**gt**`age.gt=18``age > 18`**gte**`age.gte=18``age >= 18`**lt**`price.lt=100``price < 100`**lte**`price.lte=100``price
