EasyAdmin is installed, but /admin still shows the default welcome page. Before building our first CRUD, let’s display our own dashboard, using EasyAdmin’s layout so we keep the side menu.
The Three Options in index()
Open src/Controller/Admin/DashboardController.php. The generated index() method suggests three options:
- Redirect to an existing page of the back office
- Redirect conditionally, for example depending on the logged-in user
- Render your own template, to build a real dashboard with widgets
We pick option 3. It’s simple, and it gives us full control.
Rendering Our Template
Uncomment the render() line and point it to a template in an admin/ folder:
public function index(): Response
{
// Option 3. Render a custom template
return $this->render('admin/dashboard.html.twig');
}
Creating the Template
Create the folder templates/admin/ and the file dashboard.html.twig. The template extends EasyAdmin’s content page and fills the content block:
{% extends '@EasyAdmin/page/content.html.twig' %}
{% block content %}
<h1>Dashboard</h1>
{% endblock %}
Extending @EasyAdmin/page/content.html.twig is the key: we get the whole EasyAdmin layout (header, side menu, styles) and only write the content.
Result
Refresh /admin: our dashboard is displayed, with the side menu and its Dashboard item. The icon next to it comes from the menu item we saw in the previous chapter:
yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home');
Going further: a real dashboard
A dashboard that just says “Dashboard” is a bit empty. Since index() is a regular Symfony controller, you can inject repositories and pass numbers to the template:
use App\Repository\AuthorRepository;
use App\Repository\BookRepository;
public function __construct(
private readonly BookRepository $bookRepository,
private readonly AuthorRepository $authorRepository,
) {}
public function index(): Response
{
return $this->render('admin/dashboard.html.twig', [
'nbBooks' => $this->bookRepository->count([]),
'nbAuthors' => $this->authorRepository->count([]),
]);
}
{% extends '@EasyAdmin/page/content.html.twig' %}
{% block content %}
<h1>Mini Library</h1>
<p>{{ nbBooks }} books · {{ nbAuthors }} authors</p>
{% endblock %}
count([]) counts every row in the table, with no filter.
Recap
index()decides what/admindisplays$this->render('admin/dashboard.html.twig')renders our own page- Extending
@EasyAdmin/page/content.html.twigkeeps the EasyAdmin layout
Exercise · The number of categories
Add the number of categories to the dashboard, the same way as books and authors.
Show the solution
Inject CategoryRepository in the constructor, then:
'nbCategories' => $this->categoryRepository->count([]),
And in the template:
<p>{{ nbBooks }} books · {{ nbAuthors }} authors · {{ nbCategories }} categories</p>
What’s Next
We’ll create our first CRUD, for categories, and customize its labels.
This is Chapter 6 of the Mini Library course. Follow along to build a complete Symfony 8 + EasyAdmin digital library application from scratch.