Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Hello CRUD Controller

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

The true reason to use EasyAdmin is for its CRUD controllers. Each CRUD controller will give us a rich set of pages to create, read, update, and delete a single entity. This is where EasyAdmin shines, and the next few minutes are going to be critically important to understand how EasyAdmin works. So, buckle up!

Generating the CRUD Controller

We have four entities. Let's generate a CRUD controller for Question first. Find your terminal and run:

symfony console make:admin:crud

As you can see, it recognizes our four entities. I'll hit 1 for App\Entity\Question, let this generate into the default directory... and with default namespace.

Sweet! This did exactly one thing: it created a new QuestionCrudController.php file. Let's... go open it up!

... lines 1 - 2
namespace App\Controller\Admin;
use App\Entity\Question;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class QuestionCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Question::class;
}
/*
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id'),
TextField::new('title'),
TextEditorField::new('description'),
];
}
*/
}

Linking to the CRUD Controller

Cool. But before we look too deeply into this, head over to the admin page and refresh to see... absolutely no difference! We do have a new QuestionCrudController, but these CRUD controllers are totally useless until we link to them from a dashboard. So, back over in DashboardController, down at the bottom... yield MenuItem... but instead of linkToDashboard(), there are a number of other things that we can link to. We want linkToCrud(). Pass this the label - so "Questions" - and some FontAwesome icon classes: fa fa-question-circle. Then, most importantly, pass the entity's class name: Question::class:

... lines 1 - 5
use App\Entity\Question;
... lines 7 - 15
class DashboardController extends AbstractDashboardController
{
... lines 18 - 30
public function configureMenuItems(): iterable
{
... line 33
yield MenuItem::linkToCrud('Questions', 'fa fa-question-circle', 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 can have multiple CRUD controllers for a single entity... and that's something we'll talk about later.

Okay, go refresh to reveal our new link, click and... whoa! This is amazingly cool! We have a slider for the isApproved field, which saves automatically. We also have a search bar on top... and sortable columns to help us find whatever we're looking for.

We can delete, edit... and the form even has a nice calendar widget. This is loaded with rich features out-of-the-box.

Generating All the CRUD Controllers

So let's repeat this for our other three controllers. Head back to your terminal and, once again, run:

symfony console make:admin:crud

This time generate a CRUD for Answer... with the default stuff... one for Topic with the defaults... I'll clear my screen... and finally generate one for User.

Beautiful! The only thing this did was add three more CRUD controller classes. But to make those useful, we need to link to them. I'll paste 3 more links... then customize the label, font icons and class on each of them:

... lines 1 - 4
use App\Entity\Answer;
... line 6
use App\Entity\Topic;
use App\Entity\User;
... lines 9 - 15
class DashboardController extends AbstractDashboardController
{
... lines 18 - 30
public function configureMenuItems(): iterable
{
... lines 33 - 34
yield MenuItem::linkToCrud('Answers', 'fas fa-comments', Answer::class);
yield MenuItem::linkToCrud('Topics', 'fas fa-folder', Topic::class);
yield MenuItem::linkToCrud('Users', 'fas fa-users', User::class);
}
}

Super fast!

Let's go check it out! Refresh and... look! Simply by running that command four times, we now have four different fully-featured admin sections!

The Main configure() Methods of your CRUD Controller

I want to look a little deeper into how this is working behind the scenes. Go to QuestionCrudController and look at its base class:

... lines 1 - 5
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class QuestionCrudController extends AbstractCrudController
{
... lines 10 - 24
}

Hold Cmd or Ctrl to jump into AbstractCrudController. We saw earlier that our dashboard extends AbstractDashboardController. CRUD controllers extend AbstractCrudController.

Pretty much everything about how our CRUD controller works is going to be controlled by overriding the configure methods that you see inside of here. We'll learn about all of these as we go along. But on a high level, configureCrud() helps you configure things about the CRUD section as a whole, configureAssets() allows you to add custom CSS and JavaScript to the section, and configureActions() allows you to control the actions you want, where an action is a button or link. So, you can control whether or not you have delete, edit or index links on different pages. More on that later.

The last super important method is configureFields(), which controls the fields we see on both the index page and on the form. But don't worry about those too much yet. We'll master each method along the way.

Below this, super cool... we can see the actual code that executes for each page! The index() method is the real action for the index, or "list" page. detail() is an action that shows the details of a single item, and edit() is the edit form. I love that we can see the full code that runs all of this. It'll be super useful when we're figuring out how to extend things.

But... wait a second. If you scroll back up to the configure methods, a few of these look familiar. Some of these also exist in the dashboard base controller class. And it turns out, understanding why some methods live in both classes is the key to being able to make changes to your entire admin section or changes to just one CRUD section. Let's dive into that next.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.0.2",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.11", // 1.11.99.4
        "doctrine/doctrine-bundle": "^2.1", // 2.5.5
        "doctrine/doctrine-migrations-bundle": "^3.0", // 3.2.1
        "doctrine/orm": "^2.7", // 2.10.4
        "easycorp/easyadmin-bundle": "^4.0", // v4.0.2
        "handcraftedinthealps/goodby-csv": "^1.4", // 1.4.0
        "knplabs/knp-markdown-bundle": "dev-symfony6", // dev-symfony6
        "knplabs/knp-time-bundle": "^1.11", // 1.17.0
        "sensio/framework-extra-bundle": "^6.0", // v6.2.5
        "stof/doctrine-extensions-bundle": "^1.4", // v1.7.0
        "symfony/asset": "6.0.*", // v6.0.1
        "symfony/console": "6.0.*", // v6.0.2
        "symfony/dotenv": "6.0.*", // v6.0.2
        "symfony/flex": "^2.0.0", // v2.0.1
        "symfony/framework-bundle": "6.0.*", // v6.0.2
        "symfony/mime": "6.0.*", // v6.0.2
        "symfony/monolog-bundle": "^3.0", // v3.7.1
        "symfony/runtime": "6.0.*", // v6.0.0
        "symfony/security-bundle": "6.0.*", // v6.0.2
        "symfony/stopwatch": "6.0.*", // v6.0.0
        "symfony/twig-bundle": "6.0.*", // v6.0.1
        "symfony/ux-chartjs": "^2.0", // v2.0.1
        "symfony/webpack-encore-bundle": "^1.7", // v1.13.2
        "symfony/yaml": "6.0.*", // v6.0.2
        "twig/extra-bundle": "^2.12|^3.0", // v3.3.7
        "twig/twig": "^2.12|^3.0" // v3.3.7
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.1
        "symfony/debug-bundle": "6.0.*", // v6.0.2
        "symfony/maker-bundle": "^1.15", // v1.36.4
        "symfony/var-dumper": "6.0.*", // v6.0.2
        "symfony/web-profiler-bundle": "6.0.*", // v6.0.2
        "zenstruck/foundry": "^1.1" // v1.16.0
    }
}