2

I'm using FOSRestBundle. I want to return user info but with less data information... How to do this? Here is my current response

{
    "id": 1,
    "user": {
        "id": 1,
        "username": "qqqq",
        "username_canonical": "qqqq",
        "email": "nie@mam.go",
        "email_canonical": "nie@mam.go",
        "enabled": true,
        "salt": "9ds56htrvvokkckgscs0084so8ss40g",
        "password": "ZcmowLmoNVLNfdMLakzp29G3TZKIfJWVWZ7sE3iylKin5dlYDIQfAJvDC6CFAS2PI4KcpJM0A8qPihDU6jH02A==",
        "last_login": "2016-04-03T18:45:06+0200",
        "locked": false,
        "expired": false,
        "roles": [],
        "credentials_expired": false,
        "created_at": "2016-04-03T18:45:06+0200",
        "updated_at": "2016-04-03T18:45:06+0200"
    },
    "text": "asdadadasd",
    "uv": 0,
    "dv": 0,
    "vote_count": 0,
    "public_i_p": "",
    "private_i_p": "",
    "voters": [],
    "created_at": "-0001-11-30T00:00:00+0124",
    "updated_at": "-0001-11-30T00:00:00+0124"
}

I don't want to display all user information, just username, locked, expired

My response

public function getEntryAction(Entry $entry)
{
    return View::create()
        ->setStatusCode(200)
        ->setData($entry);
}
breq
  • 24,412
  • 26
  • 65
  • 106

2 Answers2

3

I can recommend the JMSSerializerBundle. It integrates seamlessly and you can define what attributes do you want to serialize.

Bundle documentation

Annotation reference

András
  • 693
  • 5
  • 17
2

To complete the first answer:

This bundle needs a serializer to work correctly. In most cases, you'll need to enable a serializer or install one. This bundle tries the following (in the given order) to determine the serializer to use:

  • The one you configured using fos_rest.service.serializer (if you did).
  • The JMS serializer, if the JMSSerializerBundle is available (and registered).
  • The Symfony Serializer if it's enabled (or any service called serializer).

FOSRestBundle needs to use a Serializer, as described in the documentation.
The @riska recommendation is good, I never tried the built-in serializer but the JMSSerializer will fit your needs.

See exclusion strategies to expose/exclude properties permanently or dynamically.

chalasr
  • 12,971
  • 4
  • 40
  • 82