Skip to main content

Laravel 入门

¥Getting Started with Laravel

🧹 删除了集成指南

Strapi 文档团队专注于改进 Strapi 5 核心体验的文档。我们将在未来 6 个月内发布许多更改,请密切关注👀。

¥The Strapi Documentation team focuses on improving the documentation for Strapi 5's core experience. We will release many changes in the next 6 months, so please keep an eye out 👀.

因此,当前页面现在仅处于维护模式,可能未完全与 Strapi 5 保持同步,并且很快将从 docs.strapi.io 中删除并移至 Strapi 的 集成页面

¥As a result, the present page is now in maintenance mode only, might not be fully up-to-date with Strapi 5, and will soon be removed from docs.strapi.io and moved to Strapi's integrations page.

同时,如果你想帮助我们改进此页面,请随时 contribute 到文档的 GitHub 存储库。

¥In the meantime, if you want to help us improve this page, please feel free to contribute to the documentation's GitHub repository.

本集成指南遵循 快速入门指南。你应该能够通过浏览 URL http://localhost:1337/api/restaurants 使用该 API。

¥This integration guide follows the Quick Start Guide. You should be able to consume the API by browsing the URL http://localhost:1337/api/restaurants.

如果你希望使用独立的 PHP,请参阅 PHP 集成指南

¥Should you wish to use standalone PHP, see the PHP integration guide.

本指南假设你已经拥有 Laravel 已安装 并熟悉该框架的基础知识。

¥This guide assumes you already have Laravel installed and are familiar with the basics of the framework.

使用原生 Laravel Http 客户端

¥Using the native Laravel Http Client

遵循官方的 Laravel 宏文档,你可以制作一个 Strapi 宏以从 Laravel 集成到 http 客户端:

¥Following the official Laravel Macros documentation, you can make a Strapi Macro to integrate to the http client from Laravel:

App\Providers\AppServiceProvider(或你的 ServiceProvider)中:

¥In App\Providers\AppServiceProvider (or your ServiceProvider):

use Illuminate\Support\Facades\Http;

/**

* Bootstrap any application services.

* * @return void
*/
public function boot()
{
Http::macro('strapi', function () {
return Http::withHeaders([
'Authorization' => 'Bearer '. config('strapi.token'), #Token generated in the admin
])->baseUrl(config('strapi.url')); # Base url of your strapi app
});
}

config/strapi.php 中为 Strapi 创建新的配置文件:

¥Create new config file for strapi in config/strapi.php:

  return [
'url' => env('STRAPI_URL'),

'token' => env('STRAPI_TOKEN', null),
];

配置宏后,你可以从应用中的任何位置调用它,以使用指定的配置创建待处理的请求:

¥Once your macro has been configured, you may invoke it from anywhere in your application to create a pending request with the specified configuration:

# Access to GraphQL
$response = Http::strapi()->post('graphql', ['query' => $gqlQuery, 'variables' => $variables]);
#Tip you might include a .gql file here using $gqlQuery = include('gqlQuery.gql')

# Access to Api Rest
$response = Http::strapi()->get('api/pages');

安装 Laravel-Strapi Laravel 包

¥Install the Laravel-Strapi Laravel Package

composer require dbfx/laravel-strapi

这将安装 Laravel-Strapi,一个用于与 Strapi 交互的 Laravel 特定包。

¥This installs Laravel-Strapi, a Laravel specific package for interacting with Strapi.

你需要发布一个配置文件:

¥You need to publish a config file:

php artisan vendor:publish --provider="Dbfx\LaravelStrapi\LaravelStrapiServiceProvider" --tag="strapi-config"

你还需要在 .env 文件中定义 STRAPI_URLSTRAPI_CACHE_TIME

¥You also need to define your STRAPI_URL and STRAPI_CACHE_TIME in the .env file:

STRAPI_URL=http://localhost:1337
STRAPI_CACHE_TIME=3600

获取你的集合类型

¥Get your collection type

restaurant 集合类型执行 GET 请求以获取所有餐厅。

¥Execute a GET request on the restaurant collection type in order to fetch all your restaurants.

确保你激活了 restaurant 集合类型的 find 权限。

¥Be sure that you activated the find permission for the restaurant collection type.

Example GET request
$strapi = new Dbfx\LaravelStrapi();
$restaurants = $strapi->collection('restaurants');

你现在可以迭代 $restaurants 数组,其中将包含你的所有餐厅。还有更多选项可供选择:

¥You may now iterate over the $restaurants array, which will contain all your restaurants. More options are available as well:

$restaurants = $strapi->collection('restaurants', $sortKey = 'id', $sortOrder = 'DESC', $limit = 20, $start = 0, $fullUrls = true);

访问单一类型项目

¥Accessing single type items

你还可以按如下方式访问单一类型项目:

¥You may also access single type items as follows:

$strapi = new Dbfx\LaravelStrapi();

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

// Return just the ['content'] field from the homepage array
$homepageItem = $strapi->single('homepage', 'content');

按字段收集

¥Collection by field

$strapi = new Dbfx\LaravelStrapi();
$entries = $strapi->entriesByField('restaurants', 'slug', 'test-restaurant-name');

集合中的单品

¥Single item from collection

$strapi = new Dbfx\LaravelStrapi();
$entry = $strapi->entry('restaurants', $id = 5);