Skip to content

[BREAKING] Code refactoring #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
11ee44c
Upgrade to PHP v8.2 https://endoflife.date/laravel
frugan-dev Feb 23, 2024
f3eaa31
[BREAKING] Code refactoring
frugan-dev Feb 23, 2024
43d814e
Typo
frugan-dev Feb 23, 2024
222cc6f
Updated README
frugan-dev Feb 23, 2024
32dafb1
Updated README
frugan-dev Feb 23, 2024
b24f173
Removed collectionCount() method
frugan-dev Feb 24, 2024
6224e0b
Updated README
frugan-dev Feb 24, 2024
8564774
dropped support to Laravel <= 8
frugan-dev Feb 24, 2024
aa0827a
Fixed $response returned by Cache::remember() https://stackoverflow.c…
frugan-dev Feb 24, 2024
241ebb9
Updated composer
frugan-dev Feb 24, 2024
6856694
Fixed response closure
frugan-dev Feb 26, 2024
41ebbd3
fix(convertToFullUrls): use collection map
frugan-dev Feb 28, 2024
b2c9aa2
docs: add $fullUrls param
frugan-dev Feb 28, 2024
1eb9c8d
fix(cache)!: replaced cacheKey encrypt() with toBase64()
frugan-dev Mar 2, 2024
bd2ef01
feat!: support to laravel ^11
frugan-dev Dec 24, 2024
11a96cf
chore: config typo
frugan-dev Dec 25, 2024
bba6cf0
perf: type declaration in entry() method
frugan-dev Dec 27, 2024
6b8e56b
feat: add STRAPI_DEBUG in config
frugan-dev Dec 27, 2024
15f4b0b
docs: update reqs
frugan-dev Dec 30, 2024
95995fe
docs: typo
frugan-dev Dec 30, 2024
6de23c7
feat: add $response->notFound() to avoid throwing exceptions
frugan-dev Jan 3, 2025
44d89fc
feat: add support to Laravel 12
frugan-dev May 6, 2025
dfbbf1d
docs: update reqs
frugan-dev May 6, 2025
3b04c7e
fix: fix HTTP response
frugan-dev May 6, 2025
049d967
refactor!: add STRAPI_CACHE_TYPE
frugan-dev May 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

/*
* This file is part of the Laravel-Strapi helper.
* This file is part of the Laravel-Strapi wrapper.
*
* (ɔ) Dave Blakey https://github.com/dbfx
*
Expand All @@ -18,7 +18,7 @@
use PhpCsFixer\RuleSet;

$header = <<<'EOF'
This file is part of the Laravel-Strapi helper.
This file is part of the Laravel-Strapi wrapper.

(ɔ) Dave Blakey https://github.com/dbfx

Expand All @@ -39,8 +39,8 @@
->setRiskyAllowed(true)
->setRules([
// https://mlocati.github.io/php-cs-fixer-configurator
'@PHP74Migration:risky' => true,
'@PHP74Migration' => true,
'@PHP80Migration:risky' => true,
'@PHP82Migration' => true,
'@PhpCsFixer' => true,
// '@PhpCsFixer:risky' => true,
'general_phpdoc_annotation_remove' => ['annotations' => ['expectedDeprecation']],
Expand Down
147 changes: 102 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,90 +1,147 @@
# Laravel wrapper for using the Strapi headless CMS
# Laravel wrapper for the Strapi headless CMS

[![Latest Version on Packagist](https://img.shields.io/packagist/v/dbfx/laravel-strapi.svg?style=flat-square)](https://packagist.org/packages/dbfx/laravel-strapi)
[![Total Downloads](https://img.shields.io/packagist/dt/dbfx/laravel-strapi.svg?style=flat-square)](https://packagist.org/packages/dbfx/laravel-strapi)

---

Laravel-Strapi is a Laravel helper for using the Strapi headless CMS.
**Laravel-Strapi** is a Laravel wrapper for the [Strapi headless CMS](https://strapi.io/).

Note: for Strapi v3 support use version 2.x.x
## Requirements

- PHP ^8.2
- Laravel ^9 || ^10 || ^11 || ^12
- Strapi ^4 || ^5

_Note: to support Strapi v3.x use Laravel-Strapi v2.x._

## Installation

You can install the package via composer:
1\) Install the package via composer:

```bash
composer require dbfx/laravel-strapi
```

You can publish and run the migrations with:

You can publish the config file with:
2\) Publish the file `config/strapi.php` with:
```bash
php artisan vendor:publish --provider="Dbfx\LaravelStrapi\LaravelStrapiServiceProvider" --tag="strapi-config"
```

You need to define your STRAPI_URL and STRAPI_CACHE_TIME in .env:
You can also optionally define a STRAPI_TOKEN to enable authentication. Do not include 'Bearer' only the token itself.
3\) Define this mandatory configuration value in your `.env` file:

```
STRAPI_URL=https://strapi.test.com
STRAPI_CACHE_TIME=3600
STRAPI_TOKEN=abcd1234abcd1234
STRAPI_URL=https://api.example.com
```

## Usage
_Note: do not use `/api` at the end of `STRAPI_URL`._

laravel-strapi provides the collection() and entry() calls to return a full collection, or a specific entry from a collection. In the
example below we are querying the strapi collection 'blogs' and then getting the entry with id 1 from that collection.
```php
use Dbfx\LaravelStrapi\LaravelStrapi;
Optionally you can also define these values:

$strapi = new LaravelStrapi();
$blogs = $strapi->collection('blogs');
$entry = $strapi->entry('blogs', 1);
```
STRAPI_TOKEN=abcd1234abcd1234
STRAPI_CACHE_TYPE=deferred
STRAPI_CACHE_TIME=86400
STRAPI_CACHE_TTL=86400
STRAPI_FLEXIBLE_CACHE_FRESH=300
STRAPI_FLEXIBLE_CACHE_TOTAL=900
STRAPI_QUEUE_CONNECTION=redis
STRAPI_FULL_URLS=false
STRAPI_SORT_FIELD=id
STRAPI_SORT_ORDER=desc
STRAPI_PAGINATION_START=0
STRAPI_PAGINATION_LIMIT=25
STRAPI_DEBUG=false
```

There are several useful options available as well.
_Note: do not include `Bearer` in `STRAPI_TOKEN`, only the token itself._

- ```$sortKey``` and ```$sortOrder``` allow you to specify the key to sort on and the direction
- ```$fullUrls``` will automatically add your STRAPI_URL to the front of any relative URLs (e.g. images, etc).
- ```$limit``` sets how many items you are requesting
- ```$start``` is the offset to be used with limit, useful for pagination
- ```$populate``` is an array containing the fields to populate
- ```$queryData``` is an array of additional key-value pairs to add to the query string
## Usage

**Laravel-Strapi** provides these usefull methods:

```php
use Dbfx\LaravelStrapi\LaravelStrapi;
// returns collection-types rows by `$name`
$strapi->collection(string $name, array $queryParams = [], ?bool $fullUrls = null, ?int $cacheTime = null, ?string $cacheType = null);

$strapi = new LaravelStrapi();
$blogs = $strapi->collection('blogs', $sortKey = 'id', $sortOrder = 'DESC', $limit = 20, $start = 0, $fullUrls = true, $populate = ['author', 'images'], $queryData = ['locale' => 'en']);
// returns collection-types row by `$name` and `$id`
$strapi->entry(string $name, int|string $id, array $queryParams = [], ?bool $fullUrls = null, ?int $cacheTime = null, ?string $cacheType = null);

$entry = $strapi->entry('blogs', 1, $fullUrls = true, $populate = ['author', 'images'], $queryData = ['locale' => 'en']);
// returns single-types values by `$name`
$strapi->single(string $name, array $queryParams = [], ?bool $fullUrls = null, ?int $cacheTime = null, ?string $cacheType = null);
```

You may also access Single Type items as follows:

```php
use Dbfx\LaravelStrapi\LaravelStrapi;

$strapi = new LaravelStrapi();
These are all the available parameters:

// Fetch the full homepage array
$homepageArray = $strapi->single('homepage');

// Return just the ['content'] field from the homepage array
$homepageItem = $strapi->single('homepage', 'content');
```
- `$name` _(string)_: name of the collection-types (e.g. `blogs`) or single-types (e.g. `homepage`)
- `$id` _(int|string)_: id of a collection-types entry
- `$queryParams` _(array)_: optional array of key-value pairs of REST API parameters (see here https://docs.strapi.io/cms/api/rest/parameters)
- `$fullUrls` _(bool)_: optional boolean value to override the global value defined in `STRAPI_FULL_URLS` per-call
- `$cacheTime` _(int)_: optional value in seconds to override the global value defined in `STRAPI_CACHE_TIME` per-call
- `$cacheType` _(string)_: optional value (e.g. `disabled`, `normal` (default), `forever`, `deferred` and `flexible`) to override the global value defined in `STRAPI_CACHE_TYPE` per-call

And you may select entries by searching for a custom field (e.g. slug):
## Examples

```php
use Dbfx\LaravelStrapi\LaravelStrapi;

$strapi = new LaravelStrapi();

$entries = $strapi->entriesByField('blogs', 'slug', 'test-blog-post');
// returns the first 25 rows of the collection-types `blogs`
// sorted in descending order by `id`
$rows = $strapi->collection('blogs');

// same thing as before, but in `en` language,
// sorted descending by field `date` and ascending by field `title`
// and with population of nested collection-types `author` and `images`
$rows = $strapi->collection('blogs', [
'locale' => 'en',
'sort' => [
'date:desc',
'title:asc',
],
'populate' => [
'author',
'images',
],
]);

// similar to before, but with pagination by page values
$rows = $strapi->collection('blogs', [
'pagination' => [
'page' => 2,
'pageSize' => 50,
],
]);

// similar to before, but with pagination by offset values
$rows = $strapi->collection('blogs', [
'pagination' => [
'start' => 0,
'limit' => 100,
],
]);

// returns all rows of the collection-types `blogs`
// where field `slug` is equal to `test-blog-post`
$rows = $strapi->collection('blogs', [
'filters' => [
'slug' => [
'$eq' => 'test-blog-post',
],
],
'pagination' => [
'start' => 0,
'limit' => 1,
],
]);

// returns the row of the collection-types `blogs`
// where field `id` is `1`
$row = $strapi->entry('blogs', 1);

// returns all values of the single-types `homepage`
$rows = $strapi->single('homepage');
```

## Changelog
Expand Down
31 changes: 16 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dbfx/laravel-strapi",
"description": "Laravel wrapper for using the Strapi headless CMS",
"description": "Laravel wrapper for the Strapi headless CMS",
"license": "MIT",
"keywords": [
"laravel",
Expand All @@ -18,23 +18,24 @@
],
"homepage": "https://github.com/dbfx/laravel-strapi",
"require": {
"php": "^7.4 || ^8.0",
"illuminate/contracts": "^8.37 || ^9.0 || ^10",
"laravel/framework": "^8 || ^9 || ^10",
"spatie/laravel-package-tools": "^1.4.3"
"php": "^8.2",
"illuminate/contracts": "^9 || ^10 || ^11 || ^12",
"laravel/framework": "^9 || ^10 || ^11 || ^12",
"spatie/laravel-package-tools": "^1.12"
},
"require-dev": {
"brianium/paratest": "^6.2",
"ergebnis/composer-normalize": "^2.42",
"friendsofphp/php-cs-fixer": "^3.48",
"nunomaduro/collision": "^5.3 || ^6.0",
"orchestra/testbench": "^6.15",
"phpro/grumphp-shim": "^1.13",
"phpunit/phpunit": "^9.3",
"rector/rector": "^0.19.5",
"brianium/paratest": "^7.7",
"driftingly/rector-laravel": "^2.0",
"ergebnis/composer-normalize": "^2.45",
"friendsofphp/php-cs-fixer": "^3.65",
"nunomaduro/collision": "^8.5",
"orchestra/testbench": "^9.8",
"phpro/grumphp-shim": "^2.10",
"phpunit/phpunit": "^11.5",
"rector/rector": "^2.0",
"roave/security-advisories": "dev-latest",
"spatie/laravel-ray": "^1.9",
"vimeo/psalm": "^5.7"
"spatie/laravel-ray": "^1.39",
"vimeo/psalm": "^6.0@dev"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
Loading