Skip to content

Improve Dashboard controller compatibility with Symfony's #Template attribute #6789

Open
@speller

Description

Symfony allows controllers to return arrays and specify the #Template attribute to ease the use of templated responses without manual rendering. But the \EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface::index() method is currently limited to the Response object only:

interface DashboardControllerInterface
{
    ...
    public function index(): Response;
}

The suggestion is to mix the return type with the array type which will allow the dashboard index method to return an array and specify a template with the attribute:

interface DashboardControllerInterface
{
    ...
    public function index(): Response|array;
}
abstract class AbstractDashboardController extends AbstractController implements DashboardControllerInterface
{
    ...
    public function index(): Response|array
    {
        ...
    }
}

Sample dashboard controller with custom index template:

class DashboardController extends AbstractDashboardController
{
    #[Override]
    #[Template('admin/dashboard.html.twig')]
    public function index(): array
    {
        return [
            'templateVar' => 'value',
        ];
    }

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions