svnwa/laravel-strapi
最新稳定版本:v1.2
Composer 安装命令:
composer require svnwa/laravel-strapi
包简介
This package provides an implementation to consume the Strapi REST API.
README 文档
README
This package provides an implementation to consume the Strapi REST API.
The current implementation only supports fetching data from Strapi since I personally didn't have a use case to write data to it via API yet.
Currently supports Strapi v4 which is a bit different from the preceding v3 REST API.
Installation
1. Installation via Composer
composer require svnwa/laravel-strapi
2. Publishing the the config file:
php artisan vendor:publish --provider="svnwa\laravel-strapi\LaravelStrapiServiceProvider" --tag="laravel-strapi"
3. Editing the the .env file:
The STRAPI_CACHE_STORAGE_TIME is optional.1
The default value is defined in the config file at 3600 seconds (1 hour).
STRAPI_API_TOKEN=some_strapi_api_token
STRAPI_BASE_URL="https://somestrapiurl.com"
STRAPI_CACHE_STORAGE_TIME=3600
4. Optional "cache reset" route
This package also provides an optional route to automatically reset the cache with a Webhook from within Strapi e.g. after update or creation of a resource. The route can be activated in the config file. Just set the value to true:
'cacheResetRoute' => true
Usage
This package attemps to make use of a fluent api similar to Laravels Eloquent:
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();
Examples
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();
Using Strapis basic API Parameters:
Supports Strapis sorting on one or multiple fields and/or their direction
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'], ]);
Supports Strapis pagination on results by offset
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);
Supports Strapis filter results found with its "Get entries" method
GET api/restaurants?filters[name][$contains]=pizza
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->filterBy([ ['[name][$contains]','pizza'], ]);
Queries can accept a fields parameter to select only some fields.
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']);
Queries can accept a populate parameter to populate various field types
Populate with wildcard. If no parameter is given to the populate() method it defauls to using the wildcard operator:
GET api/restaurants?populate=*
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->populate();
or:
GET api/restaurants?populate[0]=menu&populate[1]=customers
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->populate(['menu','customers']);
Tip: Strapi offers deep population for e.g. nested components and does not populate them by default. Even not with the wildcard *
GET api/restaurants?populate[0]=menu.images
GET api/restaurants?publicationState=preview
Queries can accept a publicationState parameter to fetch entries based on their publication state:
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->publicationState('preview');
The locale API parameter can be used to get entries from a specific locale
GET api/restaurants?locale=de
use Svnwa\LaravelStrapi\Facades\Strapi; $listOfRestaurants = Strapi::collection('restaurants') ->locale('de');
License
MIT. Please see the license file for more information.
Footnotes
-
Time is in seconds. ↩
统计信息
- 总下载量: 4.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-24