FOSUserBundle Integration

Installing the Bundle

The installation procedure of the FOSUserBundle is described in the main Symfony docs

You can:

If you are using the API Platform Standard Edition, you will need to enable the form services in the symfony framework configuration options:

# api/config/packages/framework.yaml
    form: { enabled: true }

Creating a User Entity with Serialization Groups

Here's an example of declaration of a Doctrine ORM User class. There's also an example for a Doctrine MongoDB ODM. You need to use serialization groups to hide some properties like plainPassword (only in read) and password. The properties shown are handled with normalizationContext, while the properties you can modify are handled with denormalizationContext.

Create your User entity with serialization groups:

// api/src/Entity/User.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;

#[ORM\Table(name: 'fos_user')]
    normalizationContext: ['groups' => ['user']],
    denormalizationContext: ['groups' => ['user', 'user:write']],
class User extends BaseUser
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
    protected ?int $id = null;

    protected string $email;

    #[ORM\Column(nullable: true)] 
    protected string $fullname;

    protected string $plainPassword;

    protected string $username;

    public function setFullname(?string $fullname): void
        $this->fullname = $fullname;

    public function getFullname(): ?string
        return $this->fullname;

    public function isUser(?UserInterface $user = null): bool
        return $user instanceof self && $user->id === $this->id;

What' new?

API platform conference 2022

Sep 15,16 2022: new edition of our conference dedicated to API Platform and its ecosystem!