dbfx / laravel-strapi
Laravel 用于使用 Strapi 无头 CMS 的包装器
v4.0.1
2024-02-05 03:03 UTC
Requires
- php: ^7.4 || ^8.0
- illuminate/contracts: ^8.37 || ^9.0 || ^10
- laravel/framework: ^8 || ^9 || ^10
- spatie/laravel-package-tools: ^1.4.3
Requires (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
- roave/security-advisories: dev-latest
- spatie/laravel-ray: ^1.9
- vimeo/psalm: ^5.7
This package is auto-updated.
Last update: 2024-09-08 03:49:05 UTC
README
Laravel-Strapi 是一个用于使用 Strapi 无头 CMS 的 Laravel 辅助工具。
注意:要支持 Strapi v3,请使用 2.x.x 版本
安装
您可以通过 composer 安装此包
composer require dbfx/laravel-strapi
您可以使用以下命令发布和运行迁移
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Dbfx\LaravelStrapi\LaravelStrapiServiceProvider" --tag="strapi-config"
您需要在 .env 文件中定义 STRAPI_URL 和 STRAPI_CACHE_TIME:您还可以可选地定义 STRAPI_TOKEN 以启用身份验证。不要包含 'Bearer',只需提供令牌本身。
STRAPI_URL=https://strapi.test.com
STRAPI_CACHE_TIME=3600
STRAPI_TOKEN=abcd1234abcd1234
用法
laravel-strapi 提供了 collection() 和 entry() 调用来返回完整的集合或集合中的特定条目。以下示例中,我们正在查询 strapi 集合 'blogs',然后从该集合获取 ID 为 1 的条目。
use Dbfx\LaravelStrapi\LaravelStrapi; $strapi = new LaravelStrapi(); $blogs = $strapi->collection('blogs'); $entry = $strapi->entry('blogs', 1);
还有一些有用的选项可供使用。
$sortKey
和$sortOrder
允许您指定排序的键和方向$fullUrls
将自动将您的 STRAPI_URL 添加到任何相对 URL 的前面(例如,图片等)。$limit
设置您请求的项目数量$start
是与 limit 一起使用的偏移量,对于分页非常有用$populate
是包含要填充的字段的数组$queryData
是要添加到查询字符串中的键值对数组
use Dbfx\LaravelStrapi\LaravelStrapi; $strapi = new LaravelStrapi(); $blogs = $strapi->collection('blogs', $sortKey = 'id', $sortOrder = 'DESC', $limit = 20, $start = 0, $fullUrls = true, $populate = ['author', 'images'], $queryData = ['locale' => 'en']); $entry = $strapi->entry('blogs', 1, $fullUrls = true, $populate = ['author', 'images'], $queryData = ['locale' => 'en']);
您还可以按如下方式访问单个类型项
use Dbfx\LaravelStrapi\LaravelStrapi; $strapi = new LaravelStrapi(); // Fetch the full homepage array $homepageArray = $strapi->single('homepage'); // Return just the ['content'] field from the homepage array $homepageItem = $strapi->single('homepage', 'content');
您还可以通过搜索自定义字段(例如,slug)来选择条目
use Dbfx\LaravelStrapi\LaravelStrapi; $strapi = new LaravelStrapi(); $entries = $strapi->entriesByField('blogs', 'slug', 'test-blog-post');
变更日志
请参阅 变更日志 了解最近更改的详细信息。
贡献
请参阅 贡献指南 了解详细信息。
鸣谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。