Skip to content

Stateful 與 Stateless ​

兩種模式都能查詢、建立、更新、刪除資料;差別只有 Linker transport 是否保存元件 state。

Stateful(預設)Stateless
適合精靈、草稿、跨呼叫流程 guard後端運算、查詢、單次命令
掛載資料alias + UUID + revalias + temporary signed URL
Cache必須,且要原子鎖不建立 Linker instance
並行同 instance 鎖住;舊 rev 回 conflict同 URL 可並行;由 DB transaction、unique constraint、idempotency 保證
重整建立全新 instance,未落 DB 的 state 消失產生全新 URL
$this->set()可用沒有 state 可存

切換成 Stateless ​

php
use App\Support\Linker\LinkerMode;

class QuoteLinker extends LinkerComponent
{
    public static string $alias = 'quote';

    public static function mode(): LinkerMode
    {
        return LinkerMode::Stateless;
    }

    #[Computable]
    #[Validate(['qty' => 'required|integer|min:1', 'sku' => 'required|string'])]
    public function price(array $args): LinkerResponse
    {
        return $this->ok(['total' => Pricing::quote($args['sku'], $args['qty'])]);
    }
}

模板兩種都是 <div @linker('quote')>。

  • Stateless 不接受 mount params;範圍改放進可驗證的呼叫參數。
  • signed URL 只證明完整性與期限,不是授權。登入、CSRF、簽章 user/alias/mode、Referer、#[Computable]、#[Authorize]、#[Validate] 仍逐層檢查。

怎麼選 ​

精靈的狀態放哪裡,repo 裡有一份完整的決策書:docs/Linker/decision-wizard-state.md。/ticket/index 並排了兩個版本可以對照 Network 面板。

Stateful 的部署注意 ​

實例建議放獨立的 cache store,否則 php artisan cache:clear 會把進行中的表單一起清掉:

dotenv
LINKER_CACHE_STORE=linker

水平擴充時這個 store 要共用(Redis)。健康檢查:

bash
php artisan linker:list --health
bash
php artisan linker:list --health --allow-network-cache