Esempio n. 1
0
 /**
  * Send HTTP response
  *
  * If this is a redirect response, send the response and stop the transport handler chain.
  *
  * @link: https://en.wikipedia.org/wiki/Meta_refresh
  *
  * @param DispatcherResponseInterface $response
  * @return boolean
  */
 public function send(DispatcherResponseInterface $response)
 {
     if ($response->isRedirect()) {
         $session = $response->getUser()->getSession();
         //Set the messages into the session
         $messages = $response->getMessages();
         if (count($messages)) {
             //Auto start the session if it's not active.
             if (!$session->isActive()) {
                 $session->start();
             }
             $session->getContainer('message')->add($messages);
         }
         //Set the redirect into the response
         $format = $response->getRequest()->getFormat();
         if ($format == 'json') {
             array_unshift($messages, sprintf('Redirecting to %1$s', $response->getHeaders()->get('Location')));
             $response->setContent(json_encode(array('messages' => $messages)), 'application/json');
         }
         if ($format == 'html') {
             $response->setContent(sprintf('<!DOCTYPE html>
                     <html>
                         <head>
                             <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                             <noscript>
                                 <meta http-equiv="refresh" content="1;url=%1$s" />
                             </noscript>
                             <title>Redirecting to %1$s</title>
                         </head>
                         <body onload="window.location = \'%1$s\'">
                             Redirecting to <a href="%1$s">%1$s</a>.
                         </body>
                     </html>', htmlspecialchars($response->getHeaders()->get('Location'), ENT_QUOTES, 'UTF-8')), 'text/html');
         }
         return parent::send($response);
     }
 }
Esempio n. 2
0
 /**
  * Send HTTP headers
  *
  * @param DispatcherResponseInterface $response
  * @throws \RuntimeException If the headers have already been send
  * @return DispatcherResponseTransportAbstract
  */
 public function sendHeaders(DispatcherResponseInterface $response)
 {
     if (!headers_sent($file, $line)) {
         //Send the status header
         header(sprintf('HTTP/%s %d %s', $response->getVersion(), $response->getStatusCode(), $response->getStatusMessage()));
         //Send the other headers
         $headers = explode("\r\n", trim((string) $response->getHeaders()));
         foreach ($headers as $header) {
             header($header, false);
         }
     } else {
         throw new \RuntimeException(sprintf('Headers already send (output started at %s:%s', $file, $line));
     }
     return $this;
 }