Setting up an Admin Backend
Adding upcoming conferences to the database is the job of project admins. An admin backend is a protected section of the website where project admins can manage the website data, moderate feedback submissions, and more.
How can we create this fast? By using a bundle that is able to generate an admin backend based on the project's model. EasyAdmin fits the bill perfectly.
Installing more Dependencies
Even if the webapp
package automatically added many nice packages, for some more specific features, we need to add more dependencies. How can we add more dependencies? Via Composer. Besides "regular" Composer packages, we will work with two "special" kinds of packages:
- Symfony Components: Packages that implement core features and low level abstractions that most applications need (routing, console, HTTP client, mailer, cache, ...);
- Symfony Bundles: Packages that add high-level features or provide integrations with third-party libraries (bundles are mostly contributed by the community).
Let's add EasyAdmin as a project dependency:
1
$ symfony composer req "admin:^4"
admin
is an alias for the easycorp/easyadmin-bundle
package.
Aliases are not a Composer feature, but a concept provided by Symfony to make your life easier. Aliases are shortcuts for popular Composer packages. Want an ORM for your application? Require orm
. Want to develop an API? Require api
. These aliases are automatically resolved to one or more regular Composer packages. They are opinionated choices made by the Symfony core team.
Another neat feature is that you can always omit the symfony
vendor. Require cache
instead of symfony/cache
.
Tip
Do you remember that we mentioned a Composer plugin named symfony/flex
before? Aliases are one of its features.
Configuring EasyAdmin
EasyAdmin automatically generates an admin area for your application based on specific controllers.
To get started with EasyAdmin, let's generate a "web admin dashboard" which will be the main entry point to manage the website data:
1
$ symfony console make:admin:dashboard
Accepting the default answers creates the following controller:
By convention, all admin controllers are stored under their own App\Controller\Admin
namespace.
Access the generated admin backend at /admin
as configured by the index()
method; you can change the URL to anything you like:
Boom! We have a nice looking admin interface shell, ready to be customized to our needs.
The next step is to create controllers to manage conferences and comments.
In the dashboard controller, you might have noticed the configureMenuItems()
method which has a comment about adding links to "CRUDs". CRUD is an acronym for "Create, Read, Update, and Delete", the four basic operations you want to do on any entity. That's exactly what we want an admin to perform for us; EasyAdmin even takes it to the next level by also taking care of searching and filtering.
Let's generate a CRUD for conferences:
1
$ symfony console make:admin:crud
Select 1
to create an admin interface for conferences and use the defaults for the other questions. The following file should be generated:
Do the same for comments:
1
$ symfony console make:admin:crud
The last step is to link the conference and comment admin CRUDs to the dashboard:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
--- a/src/Controller/Admin/DashboardController.php
+++ b/src/Controller/Admin/DashboardController.php
@@ -2,6 +2,8 @@
namespace App\Controller\Admin;
+use App\Entity\Comment;
+use App\Entity\Conference;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
@@ -40,7 +42,8 @@ class DashboardController extends AbstractDashboardController
public function configureMenuItems(): iterable
{
- yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home');
- // yield MenuItem::linkToCrud('The Label', 'fas fa-list', EntityClass::class);
+ yield MenuItem::linktoRoute('Back to the website', 'fas fa-home', 'homepage');
+ yield MenuItem::linkToCrud('Conferences', 'fas fa-map-marker-alt', Conference::class);
+ yield MenuItem::linkToCrud('Comments', 'fas fa-comments', Comment::class);
}
}
We have overridden the configureMenuItems()
method to add menu items with relevant icons for conferences and comments and to add a link back to the website home page.
EasyAdmin exposes an API to ease linking to entity CRUDs via the MenuItem::linkToRoute()
method.
The main dashboard page is empty for now. This is where you can display some statistics, or any relevant information. As we don't have any important to display, let's redirect to the conference list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
--- a/src/Controller/Admin/DashboardController.php
+++ b/src/Controller/Admin/DashboardController.php
@@ -7,6 +7,7 @@ use App\Entity\Conference;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
+use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
@@ -15,7 +16,10 @@ class DashboardController extends AbstractDashboardController
#[Route('/admin', name: 'admin')]
public function index(): Response
{
- return parent::index();
+ $routeBuilder = $this->container->get(AdminUrlGenerator::class);
+ $url = $routeBuilder->setController(ConferenceCrudController::class)->generateUrl();
+
+ return $this->redirect($url);
// Option 1. You can make your dashboard redirect to some common page of your backend
//
When displaying entity relationships (the conference linked to a comment), EasyAdmin tries to use a string representation of the conference. By default, it uses a convention that uses the entity name and the primary key (like Conference #1
) if the entity does not define the "magic" __toString()
method. To make the display more meaningful, add such a method on the Conference
class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
--- a/src/Entity/Conference.php
+++ b/src/Entity/Conference.php
@@ -32,6 +32,11 @@ class Conference
$this->comments = new ArrayCollection();
}
+ public function __toString(): string
+ {
+ return $this->city.' '.$this->year;
+ }
+
public function getId(): ?int
{
return $this->id;
Do the same for the Comment
class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
--- a/src/Entity/Comment.php
+++ b/src/Entity/Comment.php
@@ -32,6 +32,11 @@ class Comment
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $photoFilename;
+ public function __toString(): string
+ {
+ return (string) $this->getEmail();
+ }
+
public function getId(): ?int
{
return $this->id;
You can now add/modify/delete conferences directly from the admin backend. Play with it and add at least one conference.
Add some comments without photos. Set the date manually for now; we will fill-in the createdAt
column automatically in a later step.
Customizing EasyAdmin
The default admin backend works well, but it can be customized in many ways to improve the experience. Let's do some simple changes to the Comment entity to demonstrate some possibilities:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
--- a/src/Controller/Admin/CommentCrudController.php
+++ b/src/Controller/Admin/CommentCrudController.php
@@ -3,7 +3,15 @@
namespace App\Controller\Admin;
use App\Entity\Comment;
+use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
+use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
+use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
+use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
+use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
+use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
+use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
+use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
class CommentCrudController extends AbstractCrudController
{
@@ -12,14 +20,44 @@ class CommentCrudController extends AbstractCrudController
return Comment::class;
}
- /*
+ public function configureCrud(Crud $crud): Crud
+ {
+ return $crud
+ ->setEntityLabelInSingular('Conference Comment')
+ ->setEntityLabelInPlural('Conference Comments')
+ ->setSearchFields(['author', 'text', 'email'])
+ ->setDefaultSort(['createdAt' => 'DESC'])
+ ;
+ }
+
+ public function configureFilters(Filters $filters): Filters
+ {
+ return $filters
+ ->add(EntityFilter::new('conference'))
+ ;
+ }
+
public function configureFields(string $pageName): iterable
{
- return [
- IdField::new('id'),
- TextField::new('title'),
- TextEditorField::new('description'),
- ];
+ yield AssociationField::new('conference');
+ yield TextField::new('author');
+ yield EmailField::new('email');
+ yield TextareaField::new('text')
+ ->hideOnIndex()
+ ;
+ yield TextField::new('photoFilename')
+ ->onlyOnIndex()
+ ;
+
+ $createdAt = DateTimeField::new('createdAt')->setFormTypeOptions([
+ 'html5' => true,
+ 'years' => range(date('Y'), date('Y') + 5),
+ 'widget' => 'single_text',
+ ]);
+ if (Crud::PAGE_EDIT === $pageName) {
+ yield $createdAt->setFormTypeOption('disabled', true);
+ } else {
+ yield $createdAt;
+ }
}
- */
}
To customize the Comment
section, listing the fields explicitly in the configureFields()
method lets us order them the way we want. Some fields are further configured, like hiding the text field on the index page.
The configureFilters()
methods defines which filters to expose on top of the search field.
These customizations are just a small introduction of the possibilities given by EasyAdmin.
Play with the admin, filter the comments by conference, or search comments by email for instance. The only issue is that anybody can access the backend. Don't worry, we will secure it in a future step.
1
$ symfony run psql -c "TRUNCATE conference RESTART IDENTITY CASCADE"
Going Further