Overview
  • Namespace
  • Class
  • Tree

Namespaces

  • Modulework
    • Modules
      • Http
        • Exceptions
        • Utilities
  • PHP

Classes

  • Cookie
  • JsonResponse
  • RedirectResponse
  • Request
  • Response
  1 <?php namespace Modulework\Modules\Http;
  2 /*
  3  * (c) Christian Gärtner <christiangaertner.film@googlemail.com>
  4  * This file is part of the Modulework Framework
  5  * License: View distributed LICENSE file
  6  */
  7 
  8 use InvalidArgumentException;
  9 use Modulework\Modules\Http\Cookie;
 10 use Modulework\Modules\Http\Utilities\HeaderWrapperInterface;
 11 
 12 /**
 13 * Redirect-Response
 14 * A HTTP header redirect
 15 */
 16 class RedirectResponse extends Response
 17 {
 18 
 19     protected $url;
 20 
 21 
 22     /**
 23      * Factory for the Response object
 24      * @param  string  $content The target URL
 25      * @param  integer $code    The HTTP status code (302 = default)
 26      * @param  array   $headers The HTTP headers (Location header is done automagical)
 27      * 
 28      * @param  \Modulework\Modules\Http\Utilities\HeaderWrapperInterface | null $headerWrapper The wrapper for PHP' s native header releated functions
 29      * 
 30      * @return \Modulework\Modules\Http\JsonResponse The new RedirectResponse object
 31      *
 32      * @throws \InvalidArgumentException (from Constructor)
 33      */
 34     public static function make($url = null, $code = 302, array $headers = array(), HeaderWrapperInterface $headerWrapper = null)
 35     {
 36         return new static($url, $code, $headers, $headerWrapper);
 37     }
 38 
 39     /**
 40      * Constructor.
 41      * @param  string  $content The target URL
 42      * @param  integer $code    The HTTP status code
 43      * @param  array   $headers The HTTP headers
 44      * 
 45      * @param  \Modulework\Modules\Http\Utilities\HeaderWrapperInterface | null $headerWrapper The wrapper for PHP' s native header releated functions
 46      * 
 47      * @return \Modulework\Modules\Http\RedirectResponse The new RedirectResponse object
 48      *
 49      * @throws \InvalidArgumentException [(from setContent, setUrl)]
 50      */
 51     public function __construct($url = null, $code = 302, array $headers = array(), HeaderWrapperInterface $headerWrapper = null)
 52     {
 53 
 54         parent::__construct('', $code, $headers, $headerWrapper);
 55 
 56         $this->setUrl($url);
 57 
 58         if (!$this->isRedirect()) {
 59             throw new InvalidArgumentException('HTTP status code not compatible with a redirect');
 60         }
 61     }
 62 
 63     /**
 64      * Set the target URL
 65      * @param string $url The target URL
 66      *
 67      * @return \Modulework\Modules\Http\RedirectResponse THIS
 68      * 
 69      * @throws \InvalidArgumentException [(from setContent)]
 70      */
 71     public function setUrl($url)
 72     {
 73         if ($url === '' || $url === null) throw new InvalidArgumentException('URL cannot be empty.');
 74         $this->url = $url;
 75 
 76         // Fallback, if the header doesn' t fire!
 77         $this->setContent(
 78                     '<!DOCTYPE html>
 79                     <html>
 80                         <head>
 81                             <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 82                             <meta http-equiv="refresh" content="1;url=' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '" />
 83 
 84                             <title>Redirect to ' . htmlspecialchars($url) . '</title>
 85                         </head>
 86                         <body>
 87                             Redirecting to <a href="%1$s">%1$s</a>...
 88                         </body>
 89                     </html>'
 90         );
 91 
 92         $this->headers->set('Location', $url, true);
 93 
 94         return $this;
 95     }
 96 
 97     /**
 98      * Returns the target URL
 99      * @return string The target URL
100      */
101     public function getUrl()
102     {
103         return $this->url;
104     }
105 
106     /**
107      * Send a cookie along the redirect
108      * @uses addCookie
109      * 
110      * @param  Cookie $cookie The cookie to send
111      * 
112      * @return \Modulework\Modules\Http\RedirectResponse THIS
113      */
114     public function withCookie(Cookie $cookie)
115     {
116         $this->addCookie($cookie);
117         return $this;
118     }
119 
120 
121 }
API documentation generated by ApiGen 2.8.0