BLOG // May 1, 2021
Laravel-Strapi package released
Laravel-Strapi is a Laravel helper for using the Strapi headless CMS, originally developed by me for the LoadForge Blog.
It makes listing, fetching, sorting, and caching Strapi CMS content extremely easy in Laravel. I've included some examples below:
// Instantiate the class
$strapi = new Dbfx\LaravelStrapi();
// Fetch all blogs
$blogs = $strapi->collection('blogs');
// Fetch a single blog ID 1
$entry = $strapi->entry('blogs', 1);
// Return just the ['content'] field from the homepage array
$homepageItem = $strapi->single('homepage', 'content');
// Fetch any blog posts with the slug `test-blog-post`
$post = $strapi->entriesByField('blogs', 'slug', 'test-blog-post');
If you'd like to contribute please feel free to create a pull request on GitHub. Otherwise you can install it on Laravel 7 or 8 as follows:
composer require dbfx/laravel-strapi
php artisan vendor:publish --tag="strapi-config"
Then add the following to your .env file:
STRAPI_URL=https://strapi.test.com
STRAPI_CACHE_TIME=3600
The STRAPI_URL is the base path to your Strapi installation (often on port 1337 so add that if needed). Should you wish to run it more securely, check Strapi with SSL and Nginx.
The STRAPI_CACHE_TIME is the time in seconds to cache Strapi results locally - important for performance.
Final words
Enjoy it, and please contribute or log any issues on the Github!
Comments
Subscribe to new articles
If you enjoy my content, consider subscribing. You will only receive new blog stories, no other email.