定制 alantiller/directus-php-sdk 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alantiller/directus-php-sdk

Composer 安装命令:

composer require alantiller/directus-php-sdk

包简介

A PHP SDK for interacting with the Directus API

README 文档

README

License: MIT

A PHP SDK for interacting with the Directus API. This SDK provides a convenient and object-oriented way to access Directus endpoints and perform common operations.

Table of Contents

Features

  • Object-oriented interface for interacting with the Directus API
  • Supports all Directus endpoints (Items, Files, Users, etc.)
  • Supports multiple authentication methods (API Key, User/Password)
  • Customizable storage for authentication tokens (Session, Cookie, Custom)
  • Easy-to-use methods for common CRUD operations (Create, Read, Update, Delete)
  • Comprehensive error handling

Requirements

  • PHP 8.0 or higher
  • Composer
  • Guzzle HTTP client (guzzlehttp/guzzle)

Installation

  1. Install the SDK using Composer:

    composer require alantiller/directus-php-sdk

Configuration

Before using the SDK, you need to configure it with your Directus base URL and authentication details.

  1. Base URL: The base URL of your Directus instance (e.g., https://your-directus-instance.com).
  2. Storage: Choose a storage mechanism for authentication tokens (Session, Cookie, or Custom).
  3. Authentication: Choose an authentication method and provide the necessary credentials.

Usage

Authentication

The SDK supports multiple authentication methods:

API Key Authentication

use AlanTiller\DirectusSdk\Directus;
use AlanTiller\DirectusSdk\Auth\ApiKeyAuth;
use AlanTiller\DirectusSdk\Storage\SessionStorage;

$baseUrl = 'https://your-directus-instance.com';
$apiKey = 'YOUR_API_KEY';

$storage = new SessionStorage('directus_'); // Optional prefix
$auth = new ApiKeyAuth($apiKey);

$directus = new Directus(
    $baseUrl,
    $storage,
    $auth
);

User/Password Authentication

use AlanTiller\DirectusSdk\Directus;
use AlanTiller\DirectusSdk\Auth\UserPasswordAuth;
use AlanTiller\DirectusSdk\Storage\SessionStorage;

$baseUrl = 'https://your-directus-instance.com';
$username = 'your_email@example.com';
$password = 'your_password';

$storage = new SessionStorage('directus_'); // Optional prefix
$auth = new UserPasswordAuth($baseUrl, $username, $password);

$directus = new Directus(
    $baseUrl,
    $storage,
    $auth
);

// Authenticate the user
try {
    $directus->authenticate();
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . PHP_EOL;
}

Items

The items endpoint allows you to manage items in a specific collection.

use AlanTiller\DirectusSdk\Directus;
use AlanTiller\DirectusSdk\Storage\SessionStorage;

$baseUrl = 'https://your-directus-instance.com';
$storage = new SessionStorage('directus_');

$directus = new Directus(
    $baseUrl,
    $storage
);

$collection = 'your_collection';
$items = $directus->items($collection);

// Get all items
$all_items = $items->get();
print_r($all_items);

// Get a specific item
$item = $items->get(1);
print_r($item);

// Create a new item
$new_item = $items->create(['name' => 'New Item', 'status' => 'published']);
print_r($new_item);

// Update an existing item
$updated_item = $items->update(['name' => 'Updated Item'], 1);
print_r($updated_item);

// Delete an item
$deleted_item = $items->delete(1);
print_r($deleted_item);

Users

The users endpoint allows you to manage users in your Directus instance.

use AlanTiller\DirectusSdk\Directus;
use AlanTiller\DirectusSdk\Storage\SessionStorage;

$baseUrl = 'https://your-directus-instance.com';
$storage = new SessionStorage('directus_');

$directus = new Directus(
    $baseUrl,
    $storage
);

$users = $directus->users();

// Get all users
$all_users = $users->get();
print_r($all_users);

// Get a specific user
$user = $users->get('user_id');
print_r($user);

// Create a new user
$new_user = $users->create([
    'first_name' => 'John',
    'last_name' => 'Doe',
    'email' => 'john.doe@example.com',
    'password' => 'password123',
    'role' => 'administrator'
]);
print_r($new_user);

// Update an existing user
$updated_user = $users->update([
    'first_name' => 'Jane',
    'last_name' => 'Doe'
], 'user_id');
print_r($updated_user);

// Delete a user
$deleted_user = $users->delete('user_id');
print_r($deleted_user);

Files

The files endpoint allows you to manage files in your Directus instance.

use AlanTiller\DirectusSdk\Directus;
use AlanTiller\DirectusSdk\Storage\SessionStorage;

$baseUrl = 'https://your-directus-instance.com';
$storage = new SessionStorage('directus_');

$directus = new Directus(
    $baseUrl,
    $storage
);

$files = $directus->files();

// Get all files
$all_files = $files->get();
print_r($all_files);

// Get a specific file
$file = $files->get('file_id');
print_r($file);

// Create a new file
$file_path = '/path/to/your/file.jpg';
$new_file = $files->create([
    'name' => basename($file_path),
    'tmp_name' => $file_path,
]);
print_r($new_file);

// Update an existing file
$updated_file = $files->update('file_id', ['title' => 'New Title']);
print_r($updated_file);

// Delete a file
$deleted_file = $files->delete('file_id');
print_r($deleted_file);

// Update complete file (uses custom call)
$file_path = '/path/to/your/file.jpg';
$this->directus->makeCustomCall(
    sprintf('/files/%s', $fileId),
    [
        'name' => basename($file_path),
        'tmp_name' => $file_path,
    ],
    'PATCH_MULTIPART'
);

Other Endpoints

The SDK provides access to all Directus endpoints, including:

  • activity()
  • collections()
  • comments()
  • contentVersions()
  • dashboards()
  • extensions()
  • fields(string $collection)
  • flows()
  • folders()
  • notifications()
  • operations()
  • panels()
  • permissions()
  • policies()
  • presets()
  • relations()
  • revisions()
  • roles()
  • schema()
  • server()
  • settings()
  • shares()
  • translations()
  • utilities()

Each endpoint provides methods for performing common operations, such as get, create, update, and delete. Refer to the Directus API documentation for more information on each endpoint and its available methods.

Custom Calls

You can make custom API calls using the makeCustomCall method:

use AlanTiller\DirectusSdk\Directus;
use AlanTiller\DirectusSdk\Storage\SessionStorage;

$baseUrl = 'https://your-directus-instance.com';
$storage = new SessionStorage('directus_');

$directus = new Directus(
    $baseUrl,
    $storage
);

$uri = '/your/custom/endpoint';
$data = ['param1' => 'value1', 'param2' => 'value2'];
$method = 'GET';

$response = $directus->makeCustomCall($uri, $data, $method);
print_r($response);

Storage

The SDK uses a StorageInterface to store authentication tokens. You can choose between session storage, cookie storage, or implement your own custom storage mechanism.

Session Storage

Session storage uses PHP sessions to store authentication tokens. This is the default storage mechanism.

use AlanTiller\DirectusSdk\Storage\SessionStorage;

$storage = new SessionStorage('directus_'); // Optional prefix

Cookie Storage

Cookie storage uses cookies to store authentication tokens.

use AlanTiller\DirectusSdk\Storage\CookieStorage;

$storage = new CookieStorage('directus_', '/'); // Optional prefix and domain

Custom Storage

You can implement your own custom storage mechanism by creating a class that implements the StorageInterface.

use AlanTiller\DirectusSdk\Storage\StorageInterface;

class MyCustomStorage implements StorageInterface
{
    public function set(string $key, $value): void
    {
        // Store the value
    }

    public function get(string $key)
    {
        // Retrieve the value
    }

    public function delete(string $key): void
    {
        // Delete the value
    }
}

$storage = new MyCustomStorage();

Error Handling

The SDK throws exceptions for API errors. You can catch these exceptions and handle them appropriately.

use AlanTiller\DirectusSdk\Directus;
use AlanTiller\DirectusSdk\Storage\SessionStorage;
use AlanTiller\DirectusSdk\Exceptions\DirectusException;

$baseUrl = 'https://your-directus-instance.com';
$storage = new SessionStorage('directus_');

$directus = new Directus(
    $baseUrl,
    $storage
);

try {
    $items = $directus->items('your_collection')->get();
    print_r($items);
} catch (DirectusException $e) {
    echo "API error: " . $e->getMessage() . PHP_EOL;
}

Testing

The SDK includes a set of Pest PHP tests to ensure that it functions correctly. To run the tests, follow these steps:

  1. Install Pest PHP:

    composer require pestphp/pest --dev
  2. Run the tests:

    ./vendor/bin/pest

Contributing

Contributions are welcome! Please submit a pull request with your changes.

License

The Directus PHP SDK is licensed under the MIT License.

alantiller/directus-php-sdk 适用场景与选型建议

alantiller/directus-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.31k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2022 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 alantiller/directus-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alantiller/directus-php-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 alantiller/directus-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 31.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 37
  • 点击次数: 11
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 4
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-13