> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# 核心套件與自訂驅動 - Feedable

> feedable-core 套件的核心輔助工具與自訂驅動的建立方式

## Feedable Core 概觀

`feedable-core` 是將 Feedable 的核心輔助工具與內建驅動分離的 Composer 套件。\
在新的 Laravel 專案中引入後，可重用與 starter kit 相同的基礎。

* 套件：[invokable/feedable-core](https://github.com/invokable/feedable-core)
* Starter kit：[invokable/feedable](https://github.com/invokable/feedable)

## Response 類別

可透過 `Rss2Response` 與 `JsonFeedResponse` 固定輸出格式。\
若採用讓使用者選擇格式的設計，請使用 `ResponseFactory`。

```php theme={null}
use Revolution\Feedable\Core\Response\Rss2Response;

return new Rss2Response(
    title: $title,
    items: $items,
);
```

```php theme={null}
use Revolution\Feedable\Core\Enums\Format;
use Revolution\Feedable\Core\Response\ResponseFactory;

Route::get('feed.{format?}', function (Format $format = Format::RSS) use ($title, $items) {
    return ResponseFactory::format($format)->make(
        title: $title,
        items: $items,
    );
});
```

## FeedItem / Author

`FeedItem` 是 RSS 與 JSON Feed 通用化的項目物件。\
`Author` 是對應 `authors` 欄位的物件，透過 `Author::make()` 建立。

```php theme={null}
use Revolution\Feedable\Core\Elements\Author;
use Revolution\Feedable\Core\Elements\FeedItem;

$item = new FeedItem(
    id: $url,
    url: $url,
    title: $title,
    summary: $summary,
    authors: [Author::make(name: $authorName)->toArray()],
);
```

## Support 輔助工具

### AbsoluteUri::resolve()

將相對路徑轉換為絕對路徑。

```php theme={null}
use Revolution\Feedable\Core\Support\AbsoluteUri;

$absolute = AbsoluteUri::resolve('https://example.com/', '/images/sample.jpg');
```

### RSS::filterLinks()

```php theme={null}
use Revolution\Feedable\Core\Support\RSS;

$xml = RSS::filterLinks($rss, $links);
```

### RSS::each()

```php theme={null}
use DOMElement;
use Revolution\Feedable\Core\Support\RSS;

$xml = RSS::each($rss, function (DOMElement $item) {
    $title = $item->getElementsByTagName('title')->item(0);

    if ($title && str_contains($title->textContent, 'NG 關鍵字')) {
        $item->parentNode?->removeChild($item);
    }
});
```

## 自訂驅動的建立方式

### 1) 直接加入 starter kit

由於 starter kit 是一般的 Laravel 應用程式，可在 `routes/web.php` 中加入路由，並以 controller（或 invokable 類別）實作。

```php theme={null}
use App\Http\Controllers\CustomFeedController;
use Revolution\Feedable\Core\Enums\Format;

Route::get('/custom/site.{format?}', CustomFeedController::class)
    ->whereIn('format', array_column(Format::cases(), 'value'));
```

### 2) 以 Composer 套件形式加入

若要在多個專案間重複使用，將 ServiceProvider 註冊路由的作法會更容易管理。

```php theme={null}
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;

class CustomDriverServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Route::middleware('web')->group(__DIR__.'/../routes/web.php');
    }
}
```

### 3) 以 Driver::about() 註冊資訊（選用）

`Driver::about()` 的註冊是為了在支援站點清單中顯示的中繼資料。\
即使未註冊，驅動本身仍可運作。

## 參考：Laravel 部落格驅動

作為實作範例，可參考 Laravel 官方部落格的驅動。

* [LaravelBlogDriver.php](https://github.com/invokable/feedable-core/blob/main/src/Drivers/Laravel/LaravelBlogDriver.php)


## Related topics

- [Feedable](/zh-TW/packages/feedable/index.md)
- [圖片加工](/zh-TW/images.md)
- [支援網站清單 - Feedable](/zh-TW/packages/feedable/drivers.md)
- [Laravel Fortify 與啟動套件](/zh-TW/advanced/fortify.md)
- [Bot 教學 - Laravel Bluesky](/zh-TW/packages/laravel-bluesky/bot-tutorial.md)
