Parameter-Specific State Providers for Hierarchical CRUDs

Aperta
#7,526 6 commenti 2 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
42/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
php, symfony
Ambito
backend

Direzione di ricerca

Inizia tracciando AdminUrlGenerator e ActionFactory per capire come vengono risolti i parametri di route obbligatori, in particolare entityId, quindi esamina l’integrazione di AdminRoute e AdminContext. Confronta il comportamento dei link index, detail, new ed edit per le route gerarchiche. Il lavoro è completo quando un provider può fornire parametri non relativi a un’entità, i valori mancanti mantengono il comportamento di eccezione esistente e i link CRUD standard preservano il contesto padre.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
Lingua principale
PHP
Stelle
4.3k
Fork
1.1k
Merge medio
2g 9h
PR unite (30g)
52

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di EasyCorp/EasyAdminBundle

Tutte le issue di EasyCorp/EasyAdminBundle

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.