Symfony\Component\HttpFoundation\Response::setContent PHP Method

setContent() public method

Valid types are strings, numbers, null, and objects that implement a __toString() method.
public setContent ( mixed $content ) : Response
$content mixed Content that can be cast to string
return Response
    public function setContent($content)
    {
        if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable(array($content, '__toString'))) {
            throw new \UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', gettype($content)));
        }

        $this->content = (string) $content;

        return $this;
    }

Usage Example

 /**
  * @Route("/edit/{user}",name="registration_admin_edit")
  */
 public function editAction(Request $request, $user)
 {
     $response = new Response();
     $em = $this->getDoctrine()->getManager();
     $form = $this->createForm(EditType::class);
     $form->handleRequest($request);
     $user = $em->getRepository(RegUser::class)->findOneByUsername($user);
     if ($form->isValid()) {
         $data = $form->getData();
         if ($data['action'] === true) {
             $password = new Password();
             $password->setHash($user->getPasswordHash());
             $password->setId('default');
             $password->setEncoder(CryptEncoder::class);
             $userLdap = $this->get('cloud.ldap.util.usermanipulator')->createUserObject($user->getUsername(), $password);
             $userLdap->setAltEmail($user->getAltEmail());
             $userLdap->setGpgPublicKey($user->getGpgPublicKey());
             $this->get('cloud.ldap.util.usermanipulator')->create($userLdap);
             $em->remove($user);
         } elseif ($data['action'] === false) {
             $em->remove($user);
         } else {
             $response->setStatusCode(400);
             return $response;
         }
         $em->flush();
     } else {
         $response->setContent(json_encode(['successfully' => false, 'error' => $form->getErrors(true)->__toString()]));
         return $response;
     }
     $response->setContent(json_encode(['successfully' => true]));
     return $response;
 }
All Usage Examples Of Symfony\Component\HttpFoundation\Response::setContent