Parameter-Specific State Providers for Hierarchical CRUDs

オープン
#7,526 コメント 6 件 リアクション 2 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
42/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
静か
技術スタック
php, symfony
領域
backend

調査の方向性

まず AdminUrlGenerator と ActionFactory を追跡し、必須のルートパラメータ、特に entityId がどのように解決されるかを確認してから、AdminRoute と AdminContext の統合を調べます。階層型ルートにおける index、detail、new、edit リンクの動作を比較します。provider がエンティティ以外のパラメータを提供でき、値が欠落している場合は既存の例外動作が維持され、標準の CRUD リンクが親コンテキストを保持できれば完了です。

索引モデルが issue の本文から書いたものです。

説明

1. The Core Problem: Mandatory Parameter Persistence

EasyAdmin's AdminUrlGenerator is hardcoded to prioritize entityId. When a developer defines a hierarchical route like this:

#[AdminRoute(path: '/{parentId}/sub-resource/{entityId}')]
class MyCrudController extends AbstractCrudController
{
    ...
}

the following occurs:

  • The Generator Error: When rendering links on the index or detail pages, the generator sees {parentId} in the route but has no source to fetch it from.
  • The Twig Crash: This results in a MissingMandatoryParametersException during the rendering of standard action buttons (Edit, Delete, etc.).
  • The Lifecycle Gap: Even if the URL is manually hacked together, the "Parent" context is lost when moving between index, new, and edit actions.
2. Proposed Solution: StateProviderInterface

I propose a single, focused interface. Its job is to resolve a specific value for a given parameter name based on the current context (the Request or the specific Entity being rendered in a row).

interface StateProviderInterface
{
    /**
     * @param string $paramName  The placeholder name (e.g., 'parentId')
     * @param AdminContext $context The current admin request context
     * @return mixed The value to be injected into the route (int, string, or object e.g Uuid::v4())
     */
    public function resolve(string $paramName, AdminContext $context): mixed;
}
3. Registration via #[AdminRoute]

The provider is linked directly to the controller via the existing AdminRoute attribute. This creates a clear contract: "If you use this route, use this provider to fill its placeholders (with the exception of entityId)."

#[AdminRoute(
    path: '/admin/{parentId}/category/{entityId}', 
    provider: CategoryStateProvider::class
)]
class CategoryCrudController extends AbstractCrudController 
{
    // ...
}

Usage Example

class CategoryStateProvider implements StateProviderInterface
{
    public function resolve(string $paramName, AdminContext $context): mixed
    {
        if ($paramName === 'parentId') {
            return $context->getEntity()->getInstance()->getParent()->getId();
        }

        return null; // This should fallback to the default exception for mandatory parameter
    }
}
4. How this Resolves the "Top-Level" Logic
A. Bulletproof URL Generation

When the ActionFactory generates a link for an EDIT action:

  1. It identifies that the route requires {parentId}.
  2. It calls the StateProvider::resolve('parentId', $context).
  3. The provider returns a value to fill the parentId (e.g., $entityDto->getInstance()->getParent()->getId()).
  4. The URL survives: The generator now has all required data to build a valid Symfony route.
B. Implicit Parent Association

Because the AdminContext now tracks the resolved parentId, standard CRUD methods become aware of the hierarchy:

  • createEntity(): Can automatically pull parentId from the context to assign the owner.
  • createIndexQueryBuilder(): Can automatically filter the list so users only see children of the current parentId.
5. Why this "Top-Level" Fix?

Instead of forcing developers to override every single action to manually inject a parentId, this solution fixes the Data Source for the AdminUrlGenerator.

By providing a single entity/value for a given parameter name, we allow EasyAdmin to handle Nested Resources and Multi-tenancy out of the box. It transforms EasyAdmin from a flat-file manager into a true hierarchical CMS.


Comparison: Current vs. Proposed
Feature Current State With StateProvider
Route Requirement Only entityId is auto-filled. parentId (or any name) is auto-filled.
Implementation Overriding 4-5 core methods. One Interface + Attribute.
Reliability Twig crashes on custom routes. Twig renders valid links dynamically.
Logic Location Scattered throughout Controller. Centralized in the Provider.
主要言語
PHP
スター
4.3k
フォーク
1.1k
平均マージ
2日 9時間
マージ済み PR(30日)
52

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

EasyCorp/EasyAdminBundle のほかの issue

EasyCorp/EasyAdminBundle の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。