Skip to content

Conversation

@Pierstoval
Copy link
Contributor

@Pierstoval Pierstoval commented Jan 24, 2025

Currently, if you create a normal Controller class and render a template that contains this code:

{% extends '@EasyAdmin/page/content.html.twig' %}

You end up having this error:

An exception has been thrown during the rendering of a template ("Call to a member function getTemplatePath() on null").

The reason for this bug is that there is no AdminContext set in the request, because the request is not served by a DashboardControllerInterface nor a CrudControllerInterface.

Considering the fact that it's possible to create an AdminContext without a CrudController (makes sense, for the main page for example), as of the signature of the AdminContextFactory::create() method:

public function create(
    Request $request,
    DashboardControllerInterface $dashboardController,
    ?CrudControllerInterface $crudController,
    ?string $actionName = null
): AdminContext

This PR allows doing things like this:

#[Route('/admin/my_route', name: 'admin_my_route', defaults: [
    EA::DASHBOARD_CONTROLLER_FQCN => DashboardController::class,
])]

And since the EA::DASHBOARD_CONTROLLER_FQCN Request attribute is only set in routes generated by EasyAdmin, there should be no conflicts with the existing codebase or with existing admins around.

And by doing so, you can create any controller and still use EasyAdmin's base templates 👌

Note: I'm using exactly this one-line change in one of my projects, so having this PR merged would be great 😁

@Pierstoval Pierstoval force-pushed the custom-controller-templates branch from 1ad1844 to a514383 Compare January 24, 2025 17:29
@pfpro
Copy link
Contributor

pfpro commented Jan 25, 2025

powerful!

@Pierstoval
Copy link
Contributor Author

Friendly ping @javiereguiluz : apparently this issue existed elsewhere, and this PR will actually fixes it 🚀

@javiereguiluz javiereguiluz added this to the 4.x milestone Apr 12, 2025
@javiereguiluz
Copy link
Collaborator

Thanks Alex! ... and sorry it took me so long to merge this.

@javiereguiluz javiereguiluz merged commit a8bef2e into EasyCorp:4.x Apr 12, 2025
12 checks passed
@Pierstoval Pierstoval deleted the custom-controller-templates branch April 27, 2025 18:46
@pfpro
Copy link
Contributor

pfpro commented Aug 10, 2025

Shouldn't this be included in the documentation, since it's now possible to use pretty URLs "globally"?

https://symfony.com/bundles/EasyAdminBundle/current/actions.html#integrating-symfony-actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants