lithium\net\http\Response::status PHP Method

status() public method

Set and get the status for the response.
public status ( string $key = null, string $status = null ) : string
$key string Optional. Set to `'code'` or `'message'` to return just the code or message of the status, otherwise returns the full status header.
$status string The code or message of the status you wish to set.
return string Returns the full HTTP status, with version, code and message or dending on $key just the code or message.
    public function status($key = null, $status = null)
    {
        if ($status === null) {
            $status = $key;
        }
        if ($status) {
            $this->status = array('code' => null, 'message' => null);
            if (is_array($status)) {
                $key = null;
                $this->status = $status + $this->status;
            } elseif (is_numeric($status) && isset($this->_statuses[$status])) {
                $this->status = array('code' => $status, 'message' => $this->_statuses[$status]);
            } else {
                $statuses = array_flip($this->_statuses);
                if (isset($statuses[$status])) {
                    $this->status = array('code' => $statuses[$status], 'message' => $status);
                }
            }
        }
        if (!isset($this->_statuses[$this->status['code']])) {
            return false;
        }
        if (isset($this->status[$key])) {
            return $this->status[$key];
        }
        return "{$this->protocol} {$this->status['code']} {$this->status['message']}";
    }

Usage Example

Example #1
0
 public function testStatus()
 {
     $response = new Response();
     $expected = 'HTTP/1.1 500 Internal Server Error';
     $result = $response->status(500);
     $this->assertEqual($expected, $result);
     $expected = 'HTTP/1.1 500 Internal Server Error';
     $result = $response->status('500');
     $this->assertEqual($expected, $result);
     $expected = 'HTTP/1.1 500 Internal Server Error';
     $result = $response->status('Internal Server Error');
     $this->assertEqual($expected, $result);
     $expected = 500;
     $result = $response->status('code', 'Internal Server Error');
     $this->assertEqual($expected, $result);
     $expected = 'Internal Server Error';
     $result = $response->status('message', 500);
     $this->assertEqual($expected, $result);
     $expected = 'HTTP/1.1 500 Internal Server Error';
     $result = $response->status();
     $this->assertEqual($expected, $result);
     $expected = 'HTTP/1.1 303 See Other';
     $result = $response->status('See Other');
     $this->assertEqual($expected, $result);
     $expected = false;
     $result = $response->status('foobar');
     $this->assertEqual($expected, $result);
 }
All Usage Examples Of lithium\net\http\Response::status