Laravel对Cloudflare Workers服务的集成。

资助包维护!
rennokki

0.1.1 2024-08-14 22:18 UTC

This package is auto-updated.

Last update: 2024-09-14 22:38:34 UTC


README

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

使用Cloudflare绑定扩展您的PHP/Laravel应用程序。

本包提供对以下功能的支持:

🚀 安装

您可以通过Composer安装此包

composer require kaffe-software/l1

🙌 使用方法

使用原生PDO的D1

虽然D1不支持通过SQL协议连接,但可以通过包连接器将其用作PDO驱动程序。这将代理查询和绑定到Cloudflare API中的D1的/query端点。

use RenokiCo\L1\D1\D1Pdo;
use RenokiCo\L1\D1\D1PdoStatement;
use RenokiCo\L1\CloudflareD1Connector;

$pdo = new D1Pdo(
    dsn: 'sqlite::memory:', // irrelevant
    connector: new CloudflareD1Connector(
        database: 'your_database_id',
        token: 'your_api_token',
        accountId: 'your_cf_account_id',
    ),
);

使用Laravel的D1

在您的config/database.php文件中添加一个新的连接

'connections' => [
    'd1' => [
        'driver' => 'd1',
        'prefix' => '',
        'database' => env('CLOUDFLARE_D1_DATABASE_ID', ''),
        'api' => 'https://api.cloudflare.com/client/v4',
        'auth' => [
            'token' => env('CLOUDFLARE_TOKEN', ''),
            'account_id' => env('CLOUDFLARE_ACCOUNT_ID', ''),
        ],
    ],
]

然后在您的.env文件中设置您的Cloudflare凭据

CLOUDFLARE_TOKEN=
CLOUDFLARE_ACCOUNT_ID=
CLOUDFLARE_D1_DATABASE_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

d1 驱动程序将代理PDO查询到Cloudflare D1 API以运行查询。

🐛 测试

启动内置的模拟Cloudflare API的Worker

cd tests/worker
npm ci
npm run start

在另一个终端中运行测试

vendor/bin/phpunit

🤝 贡献

有关详细信息,请参阅CONTRIBUTING

🔒 安全性

如果您发现任何安全相关的问题,请通过电子邮件alex@renoki.org联系,而不是使用问题跟踪器。

🎉 致谢