Symfony 4 Workshop

Admin panel

There are several options for admin panels in Symfony. Some of them are:

  • Sonata Admin Bundle. Very powerful but with a bigger learning curve than others.
  • Easy Admin Bundle. Very good usability.
  • Api Platform can be combined with an Admin generator based on React.js.

In this section we are going to use Easy Admin Bundle, since it gives us what we want and is very straightforward to install and use.

composer req admin

And edit config/packages/easy_admin.yaml to specify the entities that are going to be managed by Easy Admin:

1
2
3
4
5
6
7
8
9

easy_admin:
    entities:
        - App\Entity\Movie
        - App\Entity\Actor
        - App\Entity\Sale
        - App\Entity\Enquiry
        - App\Entity\Ticket
            

After this minimal configuration, visit http://localhost:8000/admin and you will see our admin.

In order to create or edit entries, we need to implement the method __toString() in our Entities:

In src/Entity/Movie.php:

1
2
3
4
5
6

public function __toString()
{
    return $this->getName();
}
        

In src/Entity/Actor.php:

1
2
3
4
5
6

public function __toString()
{
    return $this->getName();
}
        

In src/Entity/Ticket.php:

1
2
3
4
5
6

public function __toString()
{
    return $this->id ? (string)$this->id : '';
}
        

In src/Entity/Sale.php (it already exists from another section):

1
2
3
4
5
6

public function __toString()
{
    return $this->fullName;
}
        

In src/Entity/Enquiry.php:

1
2
3
4
5
6

public function __toString()
{
    return $this->email;
}
        

Easy Admin can be personalized with lots of configuation options (for instance, in our case we it would be great to be able to upload our own images for movies or actors). The documentation of this bundle is really good and it should be easy to adapt it to our needs.

Final exercise: start a project with just an API and an admin panel.

Exercise

During this workshop we have been installing lots of packages, because we have been doing things step by step to review the fundamentals. Let's imagine now that we want to start a new project with just an API and an admin panel. What can we do?

1. Run the command that allows you to create a new Symfony project with Symfony Flex.

2. Require API Platform, EasyAdmin and Webserver.

3. Copy (or generate with the maker bundle) the entities that we have from the main project in src/Entities.

4. Let EasyAdmin now which entities do you want to be present in you admin panel.

5. Configure your database.

6. Run the server.

Visit http://localhost:8000 and http://localhost:8000/admin . And that is it. We have a base project with an API and an admin panel in no time.

Resources for those of you who want to know about how to structure Symfony projects with DDD and/or CQRS.