Welcome Guest, Not a member yet? Register   Sign In
Serializing Doctrine objects to proper JSON content type using ResponseTrait
#1

(This post was last modified: 06-21-2022, 07:38 AM by b126.)

Dear all, 

I'm trying to return Doctrine serialized objects (thanks to JMS Serializer) as JSON.
I am extending the ResourceController (and then using ResponseTrait)

The problem is that the returned headers are of type "text/html" instead of "application/json" when i use Respond.
If I use the more classic Response->setJSON, then the header is fine.

If I well understand, Respond will always consider returning text/html when receiving a string as argument. 
How to get around this? 


PHP Code:
class Employees extends \CodeIgniter\RESTful\ResourceController
{
    public function index()
    {
        $serializer SerializerBuilder::create()->build();
        $res $serializer->serialize(getEmployeeRepo()->findAll(), 'json');

        //return $this->response->setJSON($res); //working well: application/json is returned
        return $this->respond($res); //faulty: text/html is returned
    }


Thank you
Reply


Messages In This Thread
Serializing Doctrine objects to proper JSON content type using ResponseTrait - by b126 - 06-21-2022, 06:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB