Installation and First Admin

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.

Hello! And welcome to another amazing episode of: "Recipes with Ryan". Today we'll be baking a positively mind-blowing, mile-high cinnamon bread!

Huh? It's not the recipe video? EasyAdminBundle? Ok, cool!

Um... we'll be baking a positively mind-blowing admin interface in Symfony with the wonderful EasyAdminBundle.

What about SonataAdminBundle

Wait, but what about SonataAdminBundle? SonataAdminBundle is super powerful... more powerful than EasyAdminBundle. But it's also a bit of a beast - a lot bigger and more difficult to use. If it has a feature that you need... go for it! Otherwise, jump into the easy side with EasyAdminBundle.

Code with Me!

To make your cinnamon bread a hit, code along with me. Download the course code from this page and unzip it. Inside, you'll find a directory called start/, which will have the same code you see here. Open README.md for step-by-step baking instructions... and also details on how to get your project setup!

The last step will be to find your favorite terminal, move into the project and run:

php bin/console server:run

to start the built-in web server. Find your fanciest browser and open that: http://localhost:8000. Welcome to AquaNote! The project we've been building in our Symfony series.

Actually, in that series, we spent some serious time making an admin area for one of our entities: Genus. Go to /admin/genus... and then login: username weaverryan+1@gmail.com, password: iliketurtles.

A genus is a type of animal classification. And after all our work, we can create and edit them really nicely.

That's great... but now I need an admin interface for a bunch of other entities: GenusNote, SubFamily, and User. Doing that by hand... well... that would take a while... and we've got baking to do! So instead, we'll turn to EasyAdminBundle.

Installing EasyAdminBundle

Google for EasyAdminBundle and find its GitHub page. Ah, it's made by our friend Javier! Hi Javier! Let's get this thing installed. Copy the composer require line, go back to your terminal, open a new tab, and paste:

composer require javiereguiluz/easyadmin-bundle

While Jordi is downloading that package, let's keep busy!

Copy the new bundle line, find app/AppKernel.php, and put it here!

... lines 1 - 2
use JavierEguiluz\Bundle\EasyAdminBundle\EasyAdminBundle;
... lines 4 - 6
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
... lines 12 - 25
new EasyAdminBundle(),
);
... lines 28 - 38
}
... lines 40 - 59
}

Unlike most bundles, this bundle actually gives us a new route, which we need to import. Copy the routing import lines, find our app/config/routing.yml and paste anywhere:

... lines 1 - 9
easy_admin_bundle:
resource: "@EasyAdminBundle/Controller/"
type: annotation
prefix: /easyadmin

Since we already have some pages that live under /admin, let's change the prefix to /easyadmin:

... lines 1 - 9
easy_admin_bundle:
... lines 11 - 12
prefix: /easyadmin

Finally, one last step: run the assets:install command. This should run automatically after Composer is finished... but just in case, run it again:

php bin/console assets:install --symlink

This bundle comes with some CSS and JavaScript, and we need to make sure it's available.

Setting up your First Admin

Ok, we are installed! So... what did we get for our efforts? Try going to /easyadmin. Well... it's not much to look at yet... but it will be! I promise! We just need to configure what admin sections we need... and Javier gave us a great error message about this!

In a nut shell, EasyAdminBundle can create an admin CRUD for any entity with almost zero configuration. In the docs, it shows an example of this minimal config. Copy that, find config.yml, scroll to the bottom, and paste. Change these to our entity names: AppBundle\Entity\Genus, AppBundle\Entity\GenusNote, AppBundle\Entity\SubFamily, skip GenusScientist - we'll embed that inside one of the other forms, and add AppBundle\Entity\User:

... lines 1 - 80
easy_admin:
entities:
- AppBundle\Entity\Genus
- AppBundle\Entity\GenusNote
- AppBundle\Entity\SubFamily
- AppBundle\Entity\User

We have arrived... at the moment of truth. Head back to your browser and refresh Ah, hah! Yes! A full CRUD for each entity: edit, delete, add, list, show, search, party! For a wow factor, it's even responsive: if you pop it into iPhone view, it looks pretty darn good!

This is exactly what I want for my admin interface: I want it to be amazing and I want it to let me be lazy!

Of course the trick with this bundle is learning to configure and extend it. We're 80% of the way there with no config... now let's go further.

Leave a comment!

This tutorial is built on an older version of Symfony & EasyAdminBundle. Many of the concepts are the same, but you can expect major differences in newer versions.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.3.*", // v3.3.18
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.10.3
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.5
        "symfony/swiftmailer-bundle": "^2.3", // v2.6.7
        "symfony/monolog-bundle": "^2.8", // v2.12.1
        "symfony/polyfill-apcu": "^1.0", // v1.17.0
        "sensio/distribution-bundle": "^5.0", // v5.0.25
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.29
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.4
        "knplabs/knp-markdown-bundle": "^1.4", // 1.7.1
        "doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
        "stof/doctrine-extensions-bundle": "^1.2", // v1.3.0
        "javiereguiluz/easyadmin-bundle": "^1.16" // v1.17.21
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.7
        "symfony/phpunit-bridge": "^3.0", // v3.4.40
        "nelmio/alice": "^2.1", // v2.3.5
        "doctrine/doctrine-fixtures-bundle": "^2.3" // v2.4.1
    }
}