Laravel Strapi
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.
composer require svnwa/laravel-strapi
php artisan vendor:publish --provider="svnwa\laravel-strapi\LaravelStrapiServiceProvider" --tag="laravel-strapi"
The STRAPI_CACHE_STORAGE_TIME
is optional.1
STRAPI_API_TOKEN=some_strapi_api_token
STRAPI_BASE_URL="https://somestrapiurl.com"
STRAPI_CACHE_STORAGE_TIME=3600
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
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();
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');
MIT. Please see the license file for more information.
Footnotes
-
Time is in seconds. ↩