Search code examples
phpsymfonyfosrestbundle

FOSRestBundle - response data manipulation


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);
}

Solution

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

    Bundle documentation

    Annotation reference