httpCodes() публичный метод

Queries & sets valid HTTP response codes & messages.
public httpCodes ( integer | array | null $code = null ) : mixed
$code integer | array | null If $code is an integer, then the corresponding code/message is returned if it exists, null if it does not exist. If $code is an array, then the keys are used as codes and the values as messages to add to the default HTTP codes. The codes must be integers greater than 99 and less than 1000. Keep in mind that the HTTP specification outlines that status codes begin with a digit between 1 and 5, which defines the class of response the client is to expect. Example: httpCodes(404); // returns [404 => 'Not Found'] httpCodes([ 381 => 'Unicorn Moved', 555 => 'Unexpected Minotaur' ]); // sets these new values, and returns true httpCodes([ 0 => 'Nothing Here', -1 => 'Reverse Infinity', 12345 => 'Universal Password', 'Hello' => 'World' ]); // throws an exception due to invalid codes For more on HTTP status codes see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
Результат mixed Associative array of the HTTP codes as keys, and the message strings as values, or null of the given $code does not exist.
Пример #1
6
 public function beforeDispatch(Event $event)
 {
     $event->stopPropagation();
     $response = new Response(['body' => $this->config('message')]);
     $response->httpCodes([429 => 'Too Many Requests']);
     $response->statusCode(429);
     return $response;
 }
Пример #2
0
 /**
  * beforeDispatch.
  *
  * @param Cake\Event\Event $event Event instance
  * @return mixed Cake\Network\Response when limit is reached, void otherwise
  */
 public function beforeDispatch(Event $event)
 {
     $this->_setIdentifier($event->data['request']);
     $this->_initCache();
     $this->_count = $this->_touch($event->data['request']);
     // client has not exceeded rate limit
     if ($this->_count <= $this->config('limit')) {
         $this->_setHeaders($event->data['response']);
         return;
     }
     // client has reached rate limit
     $event->stopPropagation();
     $response = new Response(['body' => $this->config('message')]);
     $response->httpCodes([429 => 'Too Many Requests']);
     $response->statusCode(429);
     return $response;
 }
Пример #3
0
 /**
  * Tests the httpCodes method
  *
  * @expectedException \InvalidArgumentException
  * @return void
  */
 public function testHttpCodes()
 {
     $response = new Response();
     $result = $response->httpCodes();
     $this->assertEquals(42, count($result));
     $result = $response->httpCodes(100);
     $expected = [100 => 'Continue'];
     $this->assertEquals($expected, $result);
     $codes = [381 => 'Unicorn Moved', 555 => 'Unexpected Minotaur'];
     $result = $response->httpCodes($codes);
     $this->assertTrue($result);
     $this->assertEquals(44, count($response->httpCodes()));
     $result = $response->httpCodes(381);
     $expected = [381 => 'Unicorn Moved'];
     $this->assertEquals($expected, $result);
     $codes = [404 => 'Sorry Bro'];
     $result = $response->httpCodes($codes);
     $this->assertTrue($result);
     $this->assertEquals(44, count($response->httpCodes()));
     $result = $response->httpCodes(404);
     $expected = [404 => 'Sorry Bro'];
     $this->assertEquals($expected, $result);
     //Throws exception
     $response->httpCodes([0 => 'Nothing Here', -1 => 'Reverse Infinity', 12345 => 'Universal Password', 'Hello' => 'World']);
 }