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

setStatusCode() public method

Sets the response status code.
public setStatusCode ( integer $code, mixed $text = null ) : Response
$code integer HTTP status code
$text mixed HTTP status text If the status text is null it will be automatically populated for the known status codes and left empty otherwise.
return Response
    public function setStatusCode($code, $text = null)
    {
        $this->statusCode = $code = (int) $code;
        if ($this->isInvalid()) {
            throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
        }

        if (null === $text) {
            $this->statusText = isset(self::$statusTexts[$code]) ? self::$statusTexts[$code] : 'unknown status';

            return $this;
        }

        if (false === $text) {
            $this->statusText = '';

            return $this;
        }

        $this->statusText = $text;

        return $this;
    }

Usage Example

Beispiel #1
0
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     if ('prod' == $this->kernel->getEnvironment()) {
         // exception object
         $exception = $event->getException();
         $navService = $this->container->get('ssone.cms.navigation');
         $logger = $this->container->get('logger');
         $logger->error($exception->getMessage());
         $host = $navService->host;
         // new Response object
         $response = new Response();
         // set response content
         $response->setContent($this->templating->render('SSoneCMSThemeBundle:' . $navService->domainTemplate, array('navigation' => $navService->templateNavigationMap, 'pageClass' => "notfound404", 'pageTitle' => "Not found 404", 'metaDescription' => $navService->metaDescription, 'content' => array("attributes" => array("template" => "404/" . $host . ".html.twig")), 'modules' => "", 'multiLanguageLinks' => "", 'exception' => $exception)));
         // HttpExceptionInterface is a special type of exception
         // that holds status code and header details
         if ($exception instanceof HttpExceptionInterface) {
             $response->setStatusCode($exception->getStatusCode());
             $response->headers->replace($exception->getHeaders());
         } else {
             $response->setStatusCode(500);
         }
         // set the new $response object to the $event
         $event->setResponse($response);
     }
 }
All Usage Examples Of Symfony\Component\HttpFoundation\Response::setStatusCode