EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
3.99k stars 1.01k forks source link

AdminUrlGenerator always includes entityId, even when it should redirect to the Index page #6251

Open nopenopenope opened 1 month ago

nopenopenope commented 1 month ago

Describe the bug I have this piece of code in my CrudController:

    private function getCostTypeCrudLink(): string
    {
        return $this->adminUrlGenerator
            ->setDashboard(AdminDashboardController::class)
            ->setController(CostTypeCrudController::class)
            ->setAction(Action::INDEX)
            ->generateUrl();
    }

This should generate a link to the Index of the CostTypeCrudController. Due to the nature of an index page, no entityId is needed to generate the link. However, if you are injecting this link into another Crud Entity and you want to access it from the respective Edit Crud View, it will inject the ID, which then leads to an error.

The generated URL looks similar to this: admin?crudAction=index&crudControllerFqcn=App%5CController%5CAdmin%5CProject%5CCostTypeCrudController&entityId=1234

But it should not include the entityId as I am targeting the Index, which does not need it. However, EasyAdmin generates the link with the EntityId inside, when accessing the link trough an Crud::EDIT active form (i.e. from another entity where I want to link to the index).

Topdown problem description:

Inside EDIT Crud -> is a link to another Index Crud -> adminUrlGenerator injects the current Edit Crud Entity ID into the Index Crud Link -> upon accessing, there is an error.

alshenetsky commented 1 month ago

Why not

   private function getCostTypeCrudLink(): string
    {
        return $this->adminUrlGenerator
            ->setDashboard(AdminDashboardController::class)
            ->setController(CostTypeCrudController::class)
            ->setAction(Action::INDEX)
            ->unset('entityId')
            ->generateUrl();
    }

?

nopenopenope commented 1 month ago

Thanks, I will give it a try, but its hacky and not really a solution to an obvious bug.

alshenetsky commented 1 month ago

@nopenopenope You may disagree with me, but in my opinion that's the way it's designed. EaAdminUrlGenerator takes the current URL as a base with the ability to override any part of it, this is useful in many places including templates etc. But what if you want to generate a URL to a 'detail' page or something? You'd have to manually set the entityId, which is a lot more work. I don't take into account the variant where we "magically" remove entityId if the Action::INDEX argument is passed - this would not be obvious to the developer for the reason described above

nopenopenope commented 1 month ago

@alshenetsky this is not about links to Edit or Detail, its about Index-calling URLs. The function is not designed to give out broken URLs, its goal is to deliver low-effort URLs that require the least amount of input from the developer. Unsetting on something that is supposed to work out of the box makes things much more complex. Its a bug, its not "designed to break". Whilst the unsetting works, its not convenient but complex. Probably it hasn't been thought trough before.

The solution to this is easy: unset internally when Action is an non-referring action. Index never requires referal informations from the leading entity. I am preparing a PR for it. :)