beecubu/php-foundation-api-rest 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

beecubu/php-foundation-api-rest

Composer 安装命令:

composer require beecubu/php-foundation-api-rest

包简介

Simple API-REST Framework based on PHP Object Foundation.

README 文档

README

Lightweight API-REST framework based on PHP Object Foundation. It provides controllers, session authentication, and an extensible user model while delegating persistence to plugins.

Core features

  • Session and CRUD controllers.
  • Extensible user model (User, OwnerUser, ApiUser, AdminUser).
  • Repository abstraction via interfaces (IUserRepository, IPInfoCacheRepository).
  • Repository bootstrap via RepositoryProvider.

Plugin system The core has no database dependency. Plugins provide concrete repository implementations and register the driver.

Available plugins:

  • php-foundation-api-rest-mongodb
  • php-foundation-api-rest-sqlite

Each plugin provides:

  • Implementations of IUserRepository and IPInfoCacheRepository.
  • A RepositoryBootstrap that registers repositories via RepositoryProvider::set(...).

Installation

Install the core and the plugin you need:

composer require beecubu/php-foundation-api-rest
composer require beecubu/php-foundation-api-rest-mongodb
# or
composer require beecubu/php-foundation-api-rest-sqlite

Technical configuration

1) Initialize repositories

MongoDB

use Beecubu\Foundation\ApiRest\MongoDB\RepositoryBootstrap;

RepositoryBootstrap::init();

SQLite

use Beecubu\Foundation\ApiRest\SQLite\RepositoryBootstrap;

RepositoryBootstrap::init();

If you want to inject custom repositories:

use Beecubu\Foundation\ApiRest\Core\Persistence\RepositoryProvider;
use Beecubu\Foundation\ApiRest\SQLite\Core\Persistence\UserDB;
use Beecubu\Foundation\ApiRest\SQLite\Core\Persistence\IPInfoCacheDB;

RepositoryProvider::set(UserDB::current(), IPInfoCacheDB::current());

2) Driver configuration

Each plugin has its own driver configuration:

MongoDB

  • Configure the MongoDB connection (URI, database, etc).
  • Review your project bootstrap where the connection is created.

SQLite

  • Ensure the SQLite file is accessible and writable.
  • The driver creates tables on demand.

3) HTTP error status codes

By default, API errors return proper HTTP 4xx/5xx status codes.

If you need to preserve the legacy behavior and keep returning HTTP 200 for errors, disable it explicitly:

define('RESPONSE_USE_HTTP_ERROR_CODES', false);

When this flag is enabled, successful responses keep their usual codes and error responses return the status code declared by each public exception.

4) Request helpers

Use Request::current() to read request body data, options, files, and query params:

use Beecubu\Foundation\ApiRest\Api\Entities\Request\Request;

$request = Request::current();

$email = $request->getDataValue('email');
$confirmed = $request->getOption('confirm', false);
$query = $request->getQueryParams();
$search = $request->getQueryParam('search');
$filter = $request->getQueryParamsAs(MyFilter::class);
$file = $request->getFile('avatar');

getDataAs() and getQueryParamsAs() require a class that extends Beecubu\Foundation\Core\Serializable.

5) User entities

To customize the user model, create a class that extends ApiUser (or User) and use it where needed.

Basic example:

use Beecubu\Foundation\ApiRest\Core\Entities\Users\ApiUser;

class MyApiUser extends ApiUser
{
    // Add your own fields or logic
}

6) Session and authentication

Session endpoints work with ApiUser. The session controller resolves the user via the configured repository.

JWT + secure double-cookie Sessions are persisted as JWTs split across two cookies for extra safety:

  • Cookie 1 stores the public part (header.payload) and is readable by JS.
  • Cookie 2 stores the signature only and is HttpOnly.

Both cookies are marked Secure in live mode, scoped to the current subdomain, and share the same expiry.
On each successful login or session refresh, a new JWT is generated and both cookies are updated.
If either cookie is missing or the JWT fails validation, the session is treated as invalid and no user is loaded.

Notes

  • The core does not require any database.
  • Repository implementations live-in plugins.
  • The same API code can run on MongoDB or SQLite by swapping the plugin.

beecubu/php-foundation-api-rest 适用场景与选型建议

beecubu/php-foundation-api-rest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 138 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 10 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 beecubu/php-foundation-api-rest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 beecubu/php-foundation-api-rest 我们能提供哪些服务?
定制开发 / 二次开发

基于 beecubu/php-foundation-api-rest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 138
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-21