Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for custom routes to use EasyAdmin's context, especially in templates #6765

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from

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:

```php
#[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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants