定制 othyn/filament-api-resources 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

othyn/filament-api-resources

最新稳定版本:v1.1.1

Composer 安装命令:

composer require othyn/filament-api-resources

包简介

A Laravel package that enables Filament to work seamlessly with API-backed resources instead of traditional Eloquent models

README 文档

README

A Laravel package that enables Filament to work seamlessly with API-backed resources instead of traditional Eloquent models. This package provides all the necessary components to build Filament admin panels that consume REST APIs. The standard approach is to reach for the superb package known as Sushi, but I've found that a bit limiting when it came to supporting the rest of Filaments feature suite, as well as pagination being an awkward solve. So with that in mind, I created this package.

Filament V4 Beta

Exciting news: Over the weekend, the new Filament V4 Beta was released with official support for 'Tables with custom data', allowing API's to be used with "... supporting features like columns, sorting, searching, pagination, and actions."

Their documentation has extensive information on how to implement custom data now officially, including detailed documentation and extensive examples of 'Using an external API as a table data source'.

I was mid way through initial development of this package, but I can't imagine it will be useful for much longer. So with that said, I'm not going to persue this further- so be warned, the code is a bit rough and ready in places. Glad to see upstream support for this use case, well done to the Filament team as always!

Features

  • 🚀 API-First: Built specifically for API-backed resources
  • 🔄 Livewire Integration: Custom synthesizer for proper Livewire state management
  • 📄 Pagination Support: Automatic pagination handling for API responses
  • 💾 Caching: Built-in response caching with configurable TTL
  • 🛠 Flexible: Configurable response structure and headers
  • 🎯 Filament Native: Works with all standard Filament features (tables, forms, actions, etc.)

Installation

Install the package via Composer:

composer require othyn/filament-api-resources

Publish the configuration file:

php artisan vendor:publish --tag=filament-api-resources-config

Configuration

Configure your API settings in config/filament-api-resources.php:

return [
    'base_url' => env('FILAMENT_API_BASE_URL', 'https://api.example.com'),
    'default_headers' => [
        'Authorization' => 'Bearer ' . env('FILAMENT_API_TOKEN', ''),
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
    ],
    // ... more configuration options
];

Add the following environment variables to your .env file:

FILAMENT_API_BASE_URL=https://your-api.com/api
FILAMENT_API_TOKEN=your-api-token

Usage

1. Create an API Model

Extend the BaseApiModel class to create your API-backed models:

<?php

namespace App\Models;

use Othyn\FilamentApiResources\Models\BaseApiModel;

class User extends BaseApiModel
{
    protected static string $endpoint = '/users';

    protected $fillable = [
        'id',
        'name',
        'email',
        'created_at',
    ];

    protected function makeInstance(array $data): self
    {
        $user = new self();

        // In production, validate the API response data here
        // to ensure data integrity before creating the instance

        $user->fill([
            'id' => $data['id'],
            'name' => $data['name'],
            'email' => $data['email'],
            'created_at' => $data['created_at'],
        ]);

        $user->exists = true;

        return $user;
    }

    public static function get(int|string $id, bool $forceCacheRefresh = false): ?self
    {
        $instance = new self();
        $response = $instance->fetchResource(
            params: ['id' => $id],
            cacheSeconds: 60,
            forceCacheRefresh: $forceCacheRefresh
        );

        if (empty($response['data'])) {
            return null;
        }

        return $instance->makeInstance($response['data']);
    }
}

2. Create Filament Resource Pages

Use the provided base classes for your Filament resource pages:

List Page:

<?php

namespace App\Filament\Resources\UserResource\Pages;

use Othyn\FilamentApiResources\Resources\Pages\ListApiRecords;
use App\Filament\Resources\UserResource;

class ListUsers extends ListApiRecords
{
    protected static string $resource = UserResource::class;
}

View Page:

<?php

namespace App\Filament\Resources\UserResource\Pages;

use Othyn\FilamentApiResources\Resources\Pages\ViewApiRecord;
use App\Filament\Resources\UserResource;

class ViewUser extends ViewApiRecord
{
    protected static string $resource = UserResource::class;
}

3. Create Your Filament Resource

Create a standard Filament resource that uses your API model:

Important: When using API resources, you must add ->paginated(), and optionally ->deferLoading() to not block page loading on the API response, to your table configuration to ensure proper pagination and loading behavior. The pagination will automatically be forwarded to API calls.

<?php

namespace App\Filament\Resources;

use App\Models\User;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class UserResource extends Resource
{
    protected static ?string $model = User::class;
    protected static ?string $navigationIcon = 'heroicon-o-users';

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id'),
                Tables\Columns\TextColumn::make('name'),
                Tables\Columns\TextColumn::make('email'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime(),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->paginated()
            ->deferLoading();
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListUsers::route('/'),
            'view' => Pages\ViewUser::route('/{record}'),
        ];
    }
}

Advanced Usage

Custom Response Structure

If your API uses a different response structure, you can customize it per model using dot notation:

class User extends BaseApiModel
{
    protected static string $totalKey = 'meta.total';        // For APIs that return total in meta.total
    protected static string $resultsKey = 'response.users';  // For APIs that return data in response.users

    // ... rest of your model
}

Custom Headers per Request

You can add custom headers for specific requests:

protected function fetchResource(array $params = [], ?int $currentPage = null, ?int $cacheSeconds = null, bool $forceCacheRefresh = false): array
{
    return $this->getApiService()->fetch(
        endpoint: static::$endpoint,
        params: $params,
        currentPage: $currentPage,
        cacheSeconds: $cacheSeconds,
        forceCacheRefresh: $forceCacheRefresh,
        headers: ['X-Custom-Header' => 'value']
    );
}

Refreshing Data

In your view pages, you can refresh data from the API manually, in the case that you've performed some form of Livewire action on the page and wish to refresh the page state and repaint:

public function refreshData(): void
{
    $this->refreshRecord(forceCacheRefresh: true);

    $this->notify('Data refreshed successfully!');
}

API Response Format

By default, the package expects your API to return responses in Laravel's standard paginated format:

{
    "data": {
        "current_page": 1,
        "data": [
            {
                "id": 1,
                "name": "John Doe",
                "email": "john@example.com"
            }
        ],
        "total": 100,
        "per_page": 15
    }
}

This matches Laravel's default API resource pagination format. You can customize the response structure keys in the configuration file if your API uses a different format.

Pagination Query Parameters

When fetching paginated data, the package automatically sends the following query parameters to your API:

  • page - The current page number (e.g., ?page=2)
  • per_page - The number of items per page (e.g., ?per_page=15)

These parameter names can be customized in the configuration file:

// config/filament-api-resources.php
'pagination_params' => [
    'page' => 'page',           // Change to 'p' if your API uses ?p=2
    'per_page' => 'per_page',   // Change to 'limit' if your API uses ?limit=15
],

Error Handling

The package includes built-in error handling for API requests. Failed requests will throw exceptions with detailed error messages. You can catch and handle these in your application as needed.

Debugging API Requests

When debugging API requests, Laravel's default behavior truncates request exception messages which can make it difficult to see the full API response. To get complete error details, you can disable request truncation in your bootstrap/app.php file:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;

return Application::configure(basePath: dirname(__DIR__))
    ->withExceptions(function (Exceptions $exceptions) {
        // Disable truncation completely for full error details
        $exceptions->dontTruncateRequestExceptions();

        // Or set a custom length (default is 100 characters)
        // $exceptions->truncateRequestExceptionsAt(260);
    })
    ->create();

This will ensure you see the complete API response in your logs and error messages, making debugging much easier.

Caching

API responses are automatically cached based on your configuration. You can:

  • Set default cache TTL in config
  • Override cache TTL per request
  • Force cache refresh when needed
  • Disable caching by setting cacheSeconds to null

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This package is open-sourced software licensed under the MIT license.

othyn/filament-api-resources 适用场景与选型建议

othyn/filament-api-resources 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 322 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rest」 「api」 「laravel」 「resources」 「livewire」 「filament」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 othyn/filament-api-resources 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-23