area17 / twill-api
Requires
- php: ^8.0
- area17/twill: ^3.0|3.x-dev
- illuminate/support: ^10.13
- laravel-json-api/laravel: ^3.0
- spatie/laravel-package-tools: ^1.11
Requires (Dev)
- fakerphp/faker: ^1.15
- friendsofphp/php-cs-fixer: ^3.0
- laravel-json-api/testing: ^2.1
- mockery/mockery: ^1.4
- nunomaduro/collision: ^7.5
- orchestra/testbench: ^8.5
- phpunit/phpunit: ^10.2
- symfony/yaml: ^6.0
This package is auto-updated.
Last update: 2024-09-07 23:46:43 UTC
README
提供只读API,用于访问Twill模型和实体,以及创建符合JSON:API规范的REST API的基本结构。此包基于并需要优秀的Laravel JSON:API包。
安装
此包需要一个已经设置好Twill的Laravel项目。
安装Laravel JSON:API
按照Laravel JSON:API 文档中的步骤操作。
更新 config/jsonapi.php
,设置您想要使用的命名空间。
- 'namespace' => 'JsonApi', + 'namespace' => 'TwillApi',
安装Twill API
composer require area17/twill-api
发布配置文件 config/twill-api.php
和迁移。应用迁移。
php artisan vendor:publish --tag=twill-api-config php artisan vendor:publish --tag=twill-api-migrations php artisan migrate
创建您的 Server.php
基础类
php artisan twill-api:server
更新 config/jsonapi.php
中的 servers
键,使用您新创建的 Server
类。
'servers' => [ - // 'v1' => \App\JsonApi\V1\Server::class, + 'v1' => \App\TwillApi\V1\Server::class, ],
如果您想使API公开(在开发初期很方便),您可以更新 Server
类中的 authorisable
方法。
function authorizable(): bool { - return true; + return false; }
在 app/Http/Kernel.php
中的 api
组中添加API中间件。下面是可用的中间件列表。
中间件
设置语言的中间件
通过在url中添加语言查询字符串来查询API。例如,https://example.com/api/v1/books?locale=fr
将返回在法语(fr)语言环境中的结果。
protected $middlewareGroups = [ // ... 'api' => [ \A17\Twill\API\Http\Middleware\SetLocale::class, // ...
从浏览器字段和功能中移除未发布内容的中间件
。
protected $middlewareGroups = [ // ... 'api' => [ \A17\Twill\API\Http\Middleware\EnableFeaturePublishedScope::class, \A17\Twill\API\Http\Middleware\EnableRelatedItemPublishedScope::class, // ...
特性
当您的模型附加了媒体或文件时,您必须添加此包中提供的特性,以便暴露到每个(默认为 mediables
和 fileables
表)的枢轴模型的关系。
如果您的模型也有子块(通过块内的重复器保存),您必须添加 HasChildBlocks
,以便API在API响应中尊重父子关系。
use A17\Twill\API\Models\Traits\HasChildBlocks; use A17\Twill\API\Models\Traits\HasFileables; use A17\Twill\API\Models\Traits\HasMediables; class Page { use HasChildBlocks, HasFileables, HasMediables; // ... }
创建资源架构和API路由
要为新的资源(模型)创建基本架构,请使用 artisan 命令 twill-api:schema
。将 Twill 模块名称作为参数传递。
php artisan twill-api:schema snippets
此命令将创建 app/TwillApi/V1/Snippets/SnippetSchema.php
。命令将显示一些说明,说明如何将新架构注册到API服务器,以及如何在 routes/api.php
中声明端点。
有关更多信息,请参阅架构部分下的Laravel JSON:API文档。
您的新端点现在可在 http://localhost/api/v1/snippets
上使用。
资源
端点
此包提供以下端点和其架构
/api/v1
/api/v1/blocks
/api/v1/blocks/{id}
/api/v1/features
/api/v1/features/{id}
/api/v1/files
/api/v1/files/{id}
/api/v1/media
/api/v1/media/{id}
/api/v1/related-items
/api/v1/related-items/{id}
/api/v1/tags
/api/v1/tags/{id}
/api/v1/settings
/api/v1/settings/{id}
/api/v1/users
/api/v1/users/{id}
块
(待完成)
php artisan twill-api:block-content text php artisan twill-api:block-content my-block-name
浏览器字段(相关项目)
(待完成)
功能
(待完成)
使用API令牌显示未发布内容
(待完成)