EasyAdmin! For an Awesomely Powerful Admin Area
So... your site needs an admin area. Do yourself a favor and skip all that custom code and jump straight into EasyAdmin bundle. Why #1? Because it'll take you a fraction of the time to build what you need. Why #2? Because it'll…
Installing EasyAdmin
Well hey friends! We are in for a treat with this tutorial! It's EasyAdmin: my
favorite admin generator for Symfony. It just... gives you so many features out of
the box. And it looks great! This shouldn't really be a surprise because its…
Linking to EasyAdmin from Twig
…to put an "Edit" button that takes us directly to the edit action for this
specific question. So... how do we generate URLs to EasyAdmin from Twig?
Open the template for this page - templates/question/show.html.twig - and find
the
. Here it is…
Assets: Custom CSS and JS
The EasyAdmin interface looks pretty great out of the box. But what if we want to
customize the way something looks? For example, if I want to change the background
on the sidebar. How can we do that?
This type of stuff can be controlled…
Admin Dashboard
Run:
git status
Installing EasyAdmin didn't do anything fancy: it doesn't have a recipe that adds
config files or a button that makes cute kittens appear. Darn. It just added itself
and registered its bundle. So simply installing the bundle didn't give…
Override all the Templates!
…few minutes ago, we also saw content.html.twig. This is a nice layout template
that you can extend if you're creating a custom page inside of EasyAdmin.
In crud/, we see the templates for the individual pages and in
field/, there's a…
Hello CRUD Controller
…Then, most importantly,
pass the entity's class name: Question::class:
Behind the scenes, when we click this new link, EasyAdmin will recognize that
there is only one CRUD controller for the entity - QuestionCrudController -
and will know to use it. And yes, in theory, we…
Custom Controller & Generating Admin URLs
…But hmm, I want to redirect back to the "detail" page in the admin.
In order to generate a URL to somewhere inside EasyAdmin, we need a special admin
URL generator service that can help add the query parameters.
Let's autowire this: AdminUrlGenerator $adminUrlGenerator…
A Global "Export" Action
There are actually three different types of actions in EasyAdmin. The first
consists of the normal actions, like Add, Edit, Delete, and Detail. These operate
on a single entity. The second type is batch actions, which operate on a
selection of entities. For example, we…
Security Voter & Entity Permissions
Thanks to ->setEntityPermission(), EasyAdmin now runs every entity in this list
through the security system, passing ADMIN_USER_EDIT for each. If we were running
this security check manually in a normal Symfony app, it would be the equivalent of
$this->isGranted('ADMIN_USER_EDIT…
Configuring Fields
…an array or an iterable. I usually
return an iterable by saying yield Field::new() and passing the property name,
like id:
When I refresh... we have "ID" and nothing else.
So EasyAdmin has many different types of fields, like text fields, boolean fields,
and…
Deep Field Configuration
…that we have inside of User is $roles, which actually stores
an array of the roles this user should have:
That's probably a good thing to include on our admin page. And fortunately,
EasyAdmin has an ArrayField!
Check it out! Say yield ArrayField::new(…
Field Configurator Logic
…a super-hero-like class where
we get to modify any field in any CRUD section from the comfort of our home.
We really do live in the future.
At this point in the process, what EasyAdmin gives us is something called
a FieldDto, which…
Dynamic Disable an Action & AdminContext
…deletes using that same condition. But it would be much simpler if we could truly
disable the DELETE action on an entity-by-entity basis. Then EasyAdmin would
naturally just... hide the "Delete" link.
To figure out how to do this, let's click into…
Custom Field JavaScript
…the question itself is in a textarea, which
is nice. But it would be even better if we could have a fancy editor that
helps with our markup.
Fortunately EasyAdmin has something just for this. In QuestionCrudController,
for the question field, instead of a textarea…
Global vs CRUD-Specific Configuration
…It starts with /admin and then has a bunch
of query parameters. It turns out that everything in EasyAdmin is handled by a
single giant route. It all runs through the DashboardController route - the /admin
route that's above index():
So when we go to…
Upload Fields
…and... see if we can get
it all working. Fortunately, EasyAdmin makes this pretty easy! It's like it's
in the name or something...
Back over in UserCrudController (it doesn't matter where, you can have this
in whatever order you want), I'm…
Fields on some Pages, not Others
…curious:
dive in and check out the code behind the scenes. It's a great way to learn
more about how EasyAdmin works on a deeper level.
Methods like ->onlyOnIndex() give us a lot of control. But also notice that
configureFields() is passed the $pageName…
Controlling the "Formatted Value"
…of it. But let's pretend that we don't want
that for some reason... like because these are meant to be tiny avatars.
Actually, EasyAdmin has a field that's made specifically for avatars. It's called
AvatarField!
Back in our code, yield AvatarField…
Creating a Custom Field
…form. A custom field is a great way to encompass a bunch of custom field
configuration in one place so you can reuse it. And creating a custom field is
pretty easy.
Down in the src/EasyAdmin/ directory, create a new PHP class called, how…
x
597