drnio/directus-v8-manager

A PHP client for interacting with the **Directus v8 API**. This package provides easy-to-use methods for performing **CRUD operations**, managing Directus collections, and handling **authentication**, making it simple to integrate Directus into your PHP applications.

Installs: 15

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/drnio/directus-v8-manager

v6.6.0 2025-10-13 18:48 UTC

This package is auto-updated.

Last update: 2025-10-13 18:51:35 UTC


README

A lightweight PHP client for interacting with the Directus v8 API. This package provides convenient helpers for authentication, token management, and CRUD operations.

Quick Links

Features

  • User authentication (login, logout, token refresh)
  • CRUD operations for collections and items
  • Pagination support for large datasets
  • Error handling via exceptions and structured responses
  • Token management (access and refresh tokens)

Requirements

  • PHP 7.4+
  • Composer
  • Running Directus v8 instance

Installation

composer require drnio/directus-v8-manager

Environment Setup

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$baseUrl = getenv('DIRECTUS_BASE_URL');
$email = getenv('DIRECTUS_EMAIL');
$password = getenv('DIRECTUS_PASSWORD');

Classes Documentation

1. DirectusAuth

Handles authentication and token management.

Methods:

  • __construct($baseUrl, $email, $password) — Initialize with API base URL and user credentials.
  • authenticate() — Authenticate the user and return access token.
  • refreshToken() — Refresh access token using refresh token.
  • logout() — Invalidate current token and logout.

Usage Example:

use Drnio\DirectusV8Manager\DirectusAuth;

$auth = new DirectusAuth($baseUrl, $email, $password);
$token = $auth->authenticate();
$newToken = $auth->refreshToken();
$auth->logout();

// Create new user
$newUser = $directus->createUser([
  'email' => 'newuser@example.com',
  'password' => 'pass1234',
  'role' => 'viewer'
]);

// Get a user
$user = $directus->getUserById(12);

// Get a user by email
$user = $directus->getUserByEmail('email@domail.com');

// Update user
$updated = $directus->updateUser(12, ['first_name' => 'John', 'last_name' => 'Doe']);

// Delete user
$directus->deleteUser(12);

2. DirectusClient

Provides CRUD operations for Directus collections/items.

Methods:

  • __construct($baseUrl, $accessToken) — Initialize client with base URL and token.
  • items($collectionName) — Returns a helper object for collection operations.
    • get($params = []) — Retrieve items with optional parameters.
    • create($data) — Create a new item in the collection.
    • update($id, $data) — Update an item by ID.
    • delete($id) — Delete an item by ID.
    • paginate($params = []) — Iterate over items page by page.

Usage Example:

use Drnio\DirectusV8Manager\DirectusClient;

$client = new DirectusClient($baseUrl, $token);

// Retrieve items
$items = $client->items('articles')->get(['limit' => 50]);

// Create item
$newItem = $client->items('articles')->create(['title' => 'Hello', 'body' => 'World']);

// Update item
$updatedItem = $client->items('articles')->update($itemId, ['title' => 'Updated']);

// Delete item
$client->items('articles')->delete($itemId);

// Pagination example
foreach ($client->items('articles')->paginate(['limit' => 100]) as $page) {
    foreach ($page['data'] as $row) {
        // Process each row
    }
}

Error Handling

Wrap API calls with try/catch:

try {
    $items = $client->items('articles')->get();
} catch (\Exception $e) {
    error_log($e->getMessage());
}

Contributing

  1. Fork repository
  2. Create feature branch
  3. Add tests and documentation
  4. Open Pull Request

Follow PSR-12 / PSR-4 coding standards.

License

MIT © Dr-Nio