boxybird/laravel-directus-api-wrapper
Composer 安装命令:
composer require boxybird/laravel-directus-api-wrapper
包简介
A collection of Laravel facades to interact with Directus CMS.
README 文档
README
A collection of Laravel facades to interact with Directus CMS.
Requirements
- Laravel 7/8
- Directus 8
Installation
You can install the package via composer:
composer require boxybird/laravel-directus-api-wrapper
Publish config file:
php artisan vendor:publish --provider="BoxyBird\Directus\Providers\DirectusServiceProvider
Add to .env:
# No trailing /
DIRECTUS_BASE_URL=http://some-directus-install.com
DIRECTUS_PROJECT_NAME=some-project-name
Core Usage
Requests: https://docs.directus.io/api/reference.html#endpoints
use BoxyBird\Directus\Facades\Api; // General API endpoint facade - 'PARAMS' and 'JWT' are optional $request = Api::request('HTTP_METHOD', 'ENDPOINT', 'PARAMS', 'JWT'); // Examples Api::request('GET', '/users'); Api::request('POST', '/items/posts', ['...params']); Api::request('DELETE', '/server/projects/my_project'); // Reference: https://docs.directus.io/api/reference.html#endpoints
Authentication: https://docs.directus.io/api/authentication.html
Per Request:
<?php use BoxyBird\Directus\Facades\Api; use BoxyBird\Directus\Facades\Auth; $auth = Auth::authenticate([ 'email' => 'some-directus-user@gmail.com', 'password' => '12345678' ]); // Get token $jwt = data_get($auth, 'data.token'); Api::request('GET', '/posts', ['...params'], $jwt); Api::request('POST', '/items/posts', ['...params'], $jwt); // etc...
Per session:
<?php use BoxyBird\Directus\Facades\Api; use BoxyBird\Directus\Facades\Auth; Route::post('/login', function (Request $request) { $auth = Auth::authenticate([ 'email' => $request->input('email'), 'password' => $request->input('password'), ]); // Handle invalid credentials if (data_get($auth, 'error')) { abort(403); } // Store in session $request->session()->put('current_user', $auth); return view('some-view'); }); Route::post('/posts', function (Request $request) { $current_user = $request->session()->get('current_user'); $jwt = data_get($current_user, 'data.token'); $created_post = Api::request('POST', '/items/posts', [ 'title' => 'Hello, World.', 'content' => 'Lorem, ipsum dolor sit amet consectetur adipisicing elit!', ], $jwt); // ... });
Globally:
In some apps you may only need to connect to Directus using a single user. So rather than passing a JWT as an argument on every request, you can use the BoxyBird\Directus\Facades\Directus helper facade.
Under the hood, each request will look to see if
Directus::getJwt()is set.
<?php use BoxyBird\Directus\Facades\Auth; use BoxyBird\Directus\Facades\Directus; $auth = Auth::authenticate([ 'email' => 'some-directus-user@gmail.com', 'password' => '12345678' ]); // Get token or '' $jwt = data_get($auth, 'data.token', ''); // Get user or [] $user = data_get($auth, 'data.user', []); // Cache token globally with 15 min ttl Directus::setJwt($jwt, 900); // Cache user globally with 15 min ttl Directus::setUser($user, 900); // Cached jwt and user helpers Directus::getJwt(); Directus::getUser(); // Reference: https://docs.directus.io/api/authentication.html#retrieve-a-temporary-access-token --- // Attempt to refresh global token $auth = Auth::refresh(); // Reference: https://docs.directus.io/api/authentication.html#refresh-a-temporary-access-token
Additional Helpers
Items: https://docs.directus.io/api/items.html
<?php use BoxyBird\Directus\Facades\Items; // Get item from collection 'posts' with id '1' $item = Items::retrieve('posts', 1); // Reference: https://docs.directus.io/api/items.html#retrieve-an-item --- // Get items from collection 'posts' $items = Items::list('posts'); // Get items from collection 'posts' with params $items = Items::list('posts', [ 'fields' => '*.*.*', 'meta' => '*', 'filter' => [ 'title' => [ 'like' => 'Hello, World.', ] ], ]); // Reference: https://docs.directus.io/api/items.html#list-the-items --- // Create new item in collection 'posts' $create = Items::create('posts', [ 'title' => 'Hello, World.', 'content' => 'Lorem, ipsum dolor sit amet consectetur adipisicing elit!', ]); // Reference: https://docs.directus.io/api/items.html#create-an-item --- // Update item in collection 'posts' with id '1' $update = Items::update('posts', 1, [ 'title' => 'Hi, World.', ]); // Reference: https://docs.directus.io/api/items.html#update-an-item --- // Delete item in collection 'posts' with id '1' $delete = Items::delete('posts', 1); // Reference: https://docs.directus.io/api/items.html#delete-an-item --- // Get all revisions in collection 'posts' with id '1' $revisions = Items::listRevisions('posts', 1); // Reference: https://docs.directus.io/api/items.html#list-item-revisions // Get revision in collection 'posts' with id '1' at offset '3' $revision = Items::retrieveRevision('posts', 1, 3); // Reference: https://docs.directus.io/api/items.html#retrieve-an-item-revision // Revert to revision in collection 'posts' with id '1' at offset '3' $revert = Items::revertRevision('posts', 1, 3); // Reference: https://docs.directus.io/api/items.html#revert-to-a-given-revision
Files: https://docs.directus.io/api/files.html
<?php use BoxyBird\Directus\Facades\Files; // Get file with id '1' $file = Files::retrieve(1); // Reference: https://docs.directus.io/api/files.html#list-the-files --- // Get files $files = Files::list(); // Reference: https://docs.directus.io/api/files.html#list-the-files --- // Create new file $create = Files::create([ 'data' => // Raw file data (multipart/form-data), base64 string of file data, or URL you want to embed. ]); // Reference: https://docs.directus.io/api/files.html#create-a-file --- // Update file 'tags' with id '1' $update = Files::update(1, [ 'tags' => ['apple', 'pear', 'banana'] ]); // Reference: https://docs.directus.io/api/files.html#update-a-file --- // Delete file with id '1' $delete = Files::delete(1); // Reference: https://docs.directus.io/api/files.html#delete-a-file --- // Get all file revisions with id '1' $revisions = File::listRevisions(1); // Reference: https://docs.directus.io/api/files.html#list-file-revisions // Get file revision with id '1' at offset '3' $revision = Files::retrieveRevision(1, 3); // Reference: https://docs.directus.io/api/files.html#retrieve-a-file-revision
Collections: https://docs.directus.io/api/collections.html
<?php use BoxyBird\Directus\Facades\Collections; // Retrieves the details of collection 'posts' $collection = Collections::retrieve('posts'); // Reference: https://docs.directus.io/api/collections.html#retrieve-a-collection --- // List of the collections available in the project. $collections = Collections::list(); // Reference: https://docs.directus.io/api/collections.html#list-collections --- // Create the new collection 'my_collection' with 2 fields $create = Collections::create([ 'collection' => 'my_collection', 'fields' => [ [ 'field' => 'id', 'type' => 'integer', 'datatype' => 'int', 'length' => 11, 'interface' => 'numeric', 'primary_key' => true, ], [ 'field' => 'name', 'type' => 'string', 'datatype' => 'VARCHAR', 'length' => 255, 'interface' => 'text-input', ] ] ]); // Reference: https://docs.directus.io/api/collections.html#create-a-collection --- // Update the collection 'my_collection' $update = Collections::update('my_collection', [ 'note' => 'This is my first collection' ]); // Reference: https://docs.directus.io/api/collections.html#update-a-collection --- // Delete the collection 'my_collection' $delete = Collections::delete('my_collection'); // Reference: https://docs.directus.io/api/collections.html#delete-a-collection
License
boxybird/laravel-directus-api-wrapper 适用场景与选型建议
boxybird/laravel-directus-api-wrapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 615 次下载、GitHub Stars 达 9, 最近一次更新时间为 2020 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「wrapper」 「laravel」 「facade」 「directus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 boxybird/laravel-directus-api-wrapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 boxybird/laravel-directus-api-wrapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 boxybird/laravel-directus-api-wrapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
A PSR-7 compatible library for making CRUD API endpoints
gv config package
A PHP (read-only) wrapper for the Git command line utility
Alfabank REST API integration
统计信息
- 总下载量: 615
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-14