svnwa / laravel-strapi
本包提供了一个实现,用于消费 Strapi REST API。
Requires
- illuminate/support: >=9
Requires (Dev)
- orchestra/testbench: >=7
- phpunit/phpunit: >=9.0
This package is auto-updated.
Last update: 2024-09-08 15:41:40 UTC
README
本包提供了一个实现,用于消费Strapi REST API。
当前实现仅支持从 Strapi 获取数据,因为我还没有用例需要通过 API 向其写入数据。
当前支持 Strapi v4,与之前的 v3 REST API 有所不同。
安装
1. 通过 Composer 安装
composer require svnwa/laravel-strapi
2. 发布配置文件
php artisan vendor:publish --provider="svnwa\laravel-strapi\LaravelStrapiServiceProvider" --tag="laravel-strapi"
3. 编辑 .env 文件
STRAPI_CACHE_STORAGE_TIME
是可选的。1
默认值在配置文件中定义为 3600 秒(1 小时)。
STRAPI_API_TOKEN=some_strapi_api_token
STRAPI_BASE_URL="https://somestrapiurl.com"
STRAPI_CACHE_STORAGE_TIME=3600
4. 可选的“缓存重置”路由
本包还提供了一个可选的路由,可以从 Strapi 内部通过 Webhook 自动重置缓存,例如在更新或创建资源后。此路由可以在配置文件中激活。只需将值设置为 true 即可
'cacheResetRoute' => true
用法
本包试图使用类似于 Laravel Eloquent 的流畅 API
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->paginate(0,20,true) ->fields(['name','adress','tel']) ->sortBy([ ['name','asc'], ] ->filterBy([ ['[name][$contains]','pizza'], ]) ->populate() ->locale('de') ->withFullUrls() ->publicationState('preview') ->get();
示例
GET
api/restaurants
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants')->get();
GET
api/restaurants/999
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::entry('restaurants',999)->get();
使用 Strapi 的基本 API 参数
支持 Strapi 在一个或多个字段及其方向上进行排序
GET
api/restaurants?sort[0]=id:asc&sort[1]=name:desc
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->sortBy([ ['id','asc'], ['name','desc'], ]);
支持 Strapi 通过偏移量对结果进行分页
GET
api/restaurants?pagination[start]=0&pagination[limit]=20&pagination[withCount]=true
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->paginate(0,20,true);
支持 Strapi 的“获取条目”方法找到的过滤结果
GET
api/restaurants?filters[name][$contains]=pizza
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->filterBy([ ['[name][$contains]','pizza'], ]);
查询可以接受一个字段参数来选择一些字段。
GET
api/restaurants?fields[0]=name&fields[1]=adress&fields[2]=tel
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->fields(['name','adress','tel']);
查询可以接受一个 populate 参数来填充各种字段类型
使用通配符进行填充。如果 populate() 方法没有提供参数,则默认使用通配符操作符:GET
api/restaurants?populate=*
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->populate();
或
GET
api/restaurants?populate[0]=menu&populate[1]=customers
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->populate(['menu','customers']);
提示:Strapi 提供了深层填充,例如嵌套组件,并且默认不进行填充。即使不是使用通配符 * GET
api/restaurants?populate[0]=menu.images
GET
api/restaurants?publicationState=preview
查询可以接受 publicationState 参数来根据其发布状态获取条目
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->publicationState('preview');
可以使用 locale API 参数获取特定语言的条目
GET
api/restaurants?locale=de
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->locale('de');
许可证
MIT。有关更多信息,请参阅许可证文件。