set() public method

Sets a header by name.
public set ( string $key, string | array $values, boolean $replace = true )
$key string The key
$values string | array The value or an array of values
$replace boolean Whether to replace the actual value or not (true by default)
Example #1
0
 /**
  * PHP does not include HTTP_AUTHORIZATION in the $_SERVER array, so this header is missing.
  * We retrieve it from apache_request_headers()
  *
  * @param HeaderBag $headers
  */
 protected function fixAuthHeader(HeaderBag $headers)
 {
     if (!$headers->has('Authorization') && function_exists('apache_request_headers')) {
         $all = apache_request_headers();
         if (isset($all['Authorization'])) {
             $headers->set('Authorization', $all['Authorization']);
         }
         if (isset($all['authorization'])) {
             $headers->set('Authorization', $all['authorization']);
         }
     }
 }
 /**
  * PHP does not include HTTP_AUTHORIZATION in the $_SERVER array, so this header is missing.
  * We retrieve it from apache_request_headers()
  *
  * @see https://github.com/symfony/symfony/issues/7170
  *
  * @param HeaderBag $headers
  */
 private static function fixAuthHeader(\Symfony\Component\HttpFoundation\HeaderBag $headers)
 {
     if (!$headers->has('Authorization') && function_exists('apache_request_headers')) {
         $all = apache_request_headers();
         if (isset($all['Authorization'])) {
             $headers->set('Authorization', $all['Authorization']);
         }
     }
 }
Example #3
0
 /**
  * @param array $headers
  */
 public function setHeaders(array $headers)
 {
     foreach ($headers as $name => $value) {
         if ($this->metadata->isPrefixedKey($name)) {
             $this->metadata->set($name, $value);
         } else {
             // make sure we store an array
             $this->headers->set($name, (array) $value);
         }
     }
 }
Example #4
0
 /**
  * Modifies the response so that it conforms to the rules defined for a redirect status code.
  *
  * @see http://tools.ietf.org/html/rfc2616#section-10.3.5
  */
 public function setRedirect($url, $status = 302)
 {
     if (empty($url)) {
         throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
     }
     $this->setStatusCode($status);
     if (!$this->isRedirect()) {
         throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
     }
     $this->headers->set('Location', $url);
     $this->setContent(sprintf('<html><head><meta http-equiv="refresh" content="1;url=%s"/></head></html>', htmlspecialchars($url, ENT_QUOTES)));
 }
 /**
  * @covers Symfony\Component\HttpFoundation\HeaderBag::contains
  */
 public function testContains()
 {
     $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
     $this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
     $this->assertTrue($bag->contains('fuzz', 'bizz'), '->contains second value');
     $this->assertFalse($bag->contains('nope', 'nope'), '->contains unknown value');
     $this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
     // Multiple values
     $bag->set('foo', 'bor', false);
     $this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
     $this->assertTrue($bag->contains('foo', 'bor'), '->contains second value');
     $this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
 }
Example #6
0
 /**
  * Sets the Vary header.
  *
  * @param string|array $headers
  * @param Boolean      $replace Whether to replace the actual value of not (true by default)
  */
 public function setVary($headers, $replace = true)
 {
     $this->headers->set('Vary', $headers, $replace);
 }
Example #7
0
 /**
  * Sets a header by name.
  *
  * @param string  $key
  * @param mixed   $values
  * @param Boolean $replace
  */
 public function set($key, $values, $replace = true)
 {
     parent::set($key, $values, $replace);
     $this->updateRequest();
 }
 /**
  * @param HeaderBag $headers
  */
 private function enforceNoCache(HeaderBag $headers)
 {
     $headers->set('cache-control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', false);
     $headers->set('Pragma', 'no-cache', false);
 }
 protected function getContainerRenderActionCached()
 {
     $headerBag = new HeaderBag();
     $headerBag->set('If-Modified-Since', false);
     $requestMock = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\Request')->disableOriginalConstructor()->getMock();
     $requestMock->expects($this->at(0))->method('get')->will($this->returnValue('test.txt'));
     $requestMock->expects($this->at(1))->method('get')->will($this->returnValue('cached_hash'));
     $requestMock->expects($this->any())->method('isMethodSafe')->will($this->returnValue(true));
     $requestMock->expects($this->any())->method('getMethod')->will($this->returnValue('GET'));
     $requestMock->expects($this->any())->method('getEtags')->will($this->returnValue(array('"cached_hash"')));
     $requestMock->headers = $headerBag;
     $container = new Container();
     $container->set('request', $requestMock);
     return $container;
 }
Example #10
0
 /**
  * {@inheritdoc}
  *
  * @api
  */
 public function set($key, $values, $replace = true)
 {
     parent::set($key, $values, $replace);
     $uniqueKey = strtr(strtolower($key), '_', '-');
     $this->headerNames[$uniqueKey] = $key;
 }
Example #11
0
 /**
  * @param \DateTime $lastModified
  */
 public function setLastModifiedDate(\DateTime $lastModified)
 {
     $this->headers->set('Last-Modified', $lastModified->format(DATE_RFC2822));
 }
 /**
  * @param HeaderBag $headers
  */
 public function fixHeaderBag(HeaderBag $headers)
 {
     if (!$headers->has('Authorization')) {
         $headers->set('Authorization', $this->getAuthorizationHeader());
     }
 }