本文整理汇总了PHP中Cake\Network\Response::statusCode方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::statusCode方法的具体用法?PHP Response::statusCode怎么用?PHP Response::statusCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Network\Response
的用法示例。
在下文中一共展示了Response::statusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: beforeDispatch
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: render
/**
* @author Gaetan SENELLE
* @return Response
*/
public function render()
{
$response = new Response();
$exception = $this->error;
$code = $this->_code($exception);
$message = $this->_message($exception, $code);
$url = $this->controller->request->here();
$isDebug = Configure::read('debug');
$response->statusCode($code);
if (method_exists($exception, 'responseHeader')) {
$this->controller->response->header($exception->responseHeader());
}
$classname = get_class($exception);
if (preg_match('@\\\\([\\w]+)$@', $classname, $matches)) {
$classname = $matches[1];
} else {
$classname = null;
}
if (!$isDebug && !$exception instanceof ApiException && !$exception instanceof HttpException) {
$classname = null;
}
$data = ['exception' => ['type' => $classname, 'message' => $message, 'url' => h($url), 'code' => $code], 'success' => false];
$response->body(json_encode($data));
$response->type('json');
return $response;
}
示例3: filterResponse
/**
* Filters the cake response to the BrowserKit one.
*
* @param \Cake\Network\Response $response Cake response.
* @return \Symfony\Component\BrowserKit\Response BrowserKit response.
*/
protected function filterResponse($response)
{
$this->cake['response'] = $response;
foreach ($response->cookie() as $cookie) {
$this->getCookieJar()->set(new Cookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httpOnly']));
}
$response->sendHeaders();
return new BrowserKitResponse($response->body(), $response->statusCode(), $response->header());
}
示例4: unauthenticated
/**
* @param \Cake\Network\Request $request Request to get authentication information from.
* @param \Cake\Network\Response $response A response object that can have headers added.
* @return bool|\Cake\Network\Response
*/
public function unauthenticated(Request $request, Response $response)
{
if ($this->_config['continue']) {
return false;
}
if (isset($this->_exception)) {
$response->statusCode($this->_exception->httpStatusCode);
$response->header($this->_exception->getHttpHeaders());
$response->body(json_encode(['error' => $this->_exception->errorType, 'message' => $this->_exception->getMessage()]));
return $response;
}
$message = __d('authenticate', 'You are not authenticated.');
throw new BadRequestException($message);
}
示例5: checkMaintenance
/**
* Main functionality to trigger maintenance mode.
* Will automatically set the appropriate headers.
*
* Tip: Check for non CLI first
*
* if (php_sapi_name() !== 'cli') {
* App::uses('MaintenanceLib', 'Setup.Lib');
* $Maintenance = new MaintenanceLib();
* $Maintenance->checkMaintenance();
* }
*
* @param string|null $ipAddress
* @param bool $exit If Response should be sent and exited.
* @return void
* @deprecated Use Maintenance DispatcherFilter
*/
public function checkMaintenance($ipAddress = null, $exit = true)
{
if ($ipAddress === null) {
$ipAddress = env('REMOTE_ADDRESS');
}
if (!$this->isMaintenanceMode($ipAddress)) {
return;
}
$Response = new Response();
$Response->statusCode(503);
$Response->header('Retry-After', DAY);
$body = __d('setup', 'Maintenance work');
$template = APP . 'Template' . DS . 'Error' . DS . $this->template;
if (file_exists($template)) {
$body = file_get_contents($template);
}
$Response->body($body);
if ($exit) {
$Response->send();
exit;
}
}
示例6: _unauthenticated
/**
* Handles unauthenticated access attempt. First the `unauthenticated()` method
* of the last authenticator in the chain will be called. The authenticator can
* handle sending response or redirection as appropriate and return `true` to
* indicate no further action is necessary. If authenticator returns null this
* method redirects user to login action. If it's an AJAX request and config
* `ajaxLogin` is specified that element is rendered else a 403 HTTP status code
* is returned.
*
* @param \Cake\Controller\Controller $controller A reference to the controller object.
* @return \Cake\Network\Response|null Null if current action is login action
* else response object returned by authenticate object or Controller::redirect().
*/
protected function _unauthenticated(Controller $controller)
{
if (empty($this->_authenticateObjects)) {
$this->constructAuthenticate();
}
$auth = end($this->_authenticateObjects);
$result = $auth->unauthenticated($this->request, $this->response);
if ($result !== null) {
return $result;
}
if (!$this->storage()->redirectUrl()) {
$this->storage()->redirectUrl($this->request->here(false));
}
if (!$controller->request->is('ajax')) {
$this->flash($this->_config['authError']);
$this->storage()->redirectUrl($controller->request->here(false));
return $controller->redirect($this->_config['loginAction']);
}
if (!empty($this->_config['ajaxLogin'])) {
$controller->viewBuilder()->templatePath('Element');
$response = $controller->render($this->_config['ajaxLogin'], $this->RequestHandler->ajaxLayout);
$response->statusCode(403);
return $response;
}
$this->response->statusCode(403);
return $this->response;
}
示例7: toPsr
/**
* Convert a CakePHP response into a PSR7 one.
*
* @param CakeResponse $response The CakePHP response to convert
* @return PsrResponse $response The equivalent PSR7 response.
*/
public static function toPsr(CakeResponse $response)
{
$status = $response->statusCode();
$headers = $response->header();
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = $response->type();
}
$body = $response->body();
$stream = 'php://memory';
if (is_string($body)) {
$stream = new Stream('php://memory', 'wb');
$stream->write($response->body());
}
if (is_callable($body)) {
$stream = new CallbackStream($body);
}
// This is horrible, but CakePHP doesn't have a getFile() method just yet.
$fileProp = new \ReflectionProperty($response, '_file');
$fileProp->setAccessible(true);
$file = $fileProp->getValue($response);
if ($file) {
$stream = new Stream($file->path, 'rb');
}
return new DiactorosResponse($stream, $status, $headers);
}
示例8: _assertStatus
/**
* Helper method for status assertions.
*
* @param int $min Min status code.
* @param int $max Max status code.
* @param string $message The error message.
* @return void
*/
protected function _assertStatus($min, $max, $message)
{
if (!$this->_response) {
$this->fail('No response set, cannot assert status code.');
}
$status = $this->_response->statusCode();
$this->assertGreaterThanOrEqual($min, $status, $message);
$this->assertLessThanOrEqual($max, $status, $message);
}
示例9: beforeDispatch
/**
* 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;
}
示例10: unauthenticated
/**
* @param \Cake\Network\Request $request Request to get authentication information from.
* @param \Cake\Network\Response $response A response object that can have headers added.
* @return bool|\Cake\Network\Response
*/
public function unauthenticated(Request $request, Response $response)
{
if ($this->_config['continue']) {
return null;
}
if (isset($this->_exception)) {
$response->statusCode($this->_exception->httpStatusCode);
//add : to http code for cakephp (header method in Network/Response expects header separated with colon notation)
$headers = $this->_exception->getHttpHeaders();
$code = (string) $this->_exception->httpStatusCode;
$headers = array_map(function ($header) use($code) {
$pos = strpos($header, $code);
if ($pos !== false) {
return substr($header, 0, $pos + strlen($code)) . ':' . substr($header, $pos + strlen($code) + 1);
}
return $header;
}, $headers);
$response->header($headers);
$response->body(json_encode(['error' => $this->_exception->errorType, 'message' => $this->_exception->getMessage()]));
return $response;
}
$message = __d('authenticate', 'You are not authenticated.');
throw new BadRequestException($message);
}
示例11: testStatusCode
/**
* Tests the statusCode method
*
* @expectedException \InvalidArgumentException
* @return void
*/
public function testStatusCode()
{
$response = new Response();
$this->assertEquals(200, $response->statusCode());
$response->statusCode(404);
$this->assertEquals(404, $response->statusCode());
$this->assertEquals(500, $response->statusCode(500));
//Throws exception
$response->statusCode(1001);
}
示例12: beforeRedirect
/**
* Handles (fakes) redirects for AJAX requests using requestAction()
*
* @param Event $event The Controller.beforeRedirect event.
* @param string|array $url A string or array containing the redirect location
* @param \Cake\Network\Response $response The response object.
* @return void|\Cake\Network\Response The response object if the redirect is caught.
*/
public function beforeRedirect(Event $event, $url, Response $response)
{
$request = $this->request;
if (!$request->is('ajax')) {
return;
}
if (empty($url)) {
return;
}
if (is_array($url)) {
$url = Router::url($url + ['_base' => false]);
}
$controller = $event->subject();
$response->body($controller->requestAction($url, ['return', 'bare' => false, 'environment' => ['REQUEST_METHOD' => 'GET']]));
$response->statusCode(200);
return $response;
}
示例13: _unauthenticated
/**
* Handles unauthenticated access attempt. First the `unauthenticated()` method
* of the last authenticator in the chain will be called. The authenticator can
* handle sending response or redirection as appropriate and return `true` to
* indicate no further action is necessary. If authenticator returns null this
* method redirects user to login action. If it's an AJAX request and config
* `ajaxLogin` is specified that element is rendered else a 403 HTTP status code
* is returned.
*
* @param \Cake\Controller\Controller $controller A reference to the controller object.
* @return void|\Cake\Network\Response Null if current action is login action
* else response object returned by authenticate object or Controller::redirect().
*/
protected function _unauthenticated(Controller $controller)
{
if (empty($this->_authenticateObjects)) {
$this->constructAuthenticate();
}
$auth = end($this->_authenticateObjects);
$result = $auth->unauthenticated($this->request, $this->response);
if ($result !== null) {
return $result;
}
if ($this->_isLoginAction($controller)) {
if (empty($controller->request->data) && !$this->session->check('Auth.redirect') && $this->request->env('HTTP_REFERER')) {
$this->session->write('Auth.redirect', $controller->referer(null, true));
}
return;
}
if (!$controller->request->is('ajax')) {
$this->flash($this->_config['authError']);
$this->session->write('Auth.redirect', $controller->request->here(false));
return $controller->redirect($this->_config['loginAction']);
}
if (!empty($this->_config['ajaxLogin'])) {
$controller->viewPath = 'Element';
$response = $controller->render($this->_config['ajaxLogin'], $this->RequestHandler->ajaxLayout);
$response->statusCode(403);
return $response;
}
$this->response->statusCode(403);
return $this->response;
}
示例14: setContentType
/**
* Add in the Content-Type header if necessary.
*
* @param array $headers The headers to update
* @param \Cake\Network\Response $response The CakePHP response to convert
* @return array The updated headers.
*/
protected static function setContentType($headers, $response)
{
if (isset($headers['Content-Type'])) {
return $headers;
}
if (in_array($response->statusCode(), [204, 304])) {
return $headers;
}
$whitelist = ['application/javascript', 'application/json', 'application/xml', 'application/rss+xml'];
$type = $response->type();
$charset = $response->charset();
$hasCharset = false;
if ($charset && (strpos($type, 'text/') === 0 || in_array($type, $whitelist))) {
$hasCharset = true;
}
$value = $type;
if ($hasCharset) {
$value = "{$type}; charset={$charset}";
}
$headers['Content-Type'] = $value;
return $headers;
}
示例15: beforeRedirect
/**
* Handles (fakes) redirects for AJAX requests using requestAction()
*
* @param Event $event The Controller.beforeRedirect event.
* @param string|array $url A string or array containing the redirect location
* @param \Cake\Network\Response $response The response object.
* @return \Cake\Network\Response|null The response object if the redirect is caught.
*/
public function beforeRedirect(Event $event, $url, Response $response)
{
$request = $this->request;
if (!$request->is('ajax')) {
return null;
}
if (empty($url)) {
return null;
}
if (is_array($url)) {
$url = Router::url($url + ['_base' => false]);
}
$query = [];
if (strpos($url, '?') !== false) {
list($url, $querystr) = explode('?', $url, 2);
parse_str($querystr, $query);
}
$controller = $event->subject();
$response->body($controller->requestAction($url, ['return', 'bare' => false, 'environment' => ['REQUEST_METHOD' => 'GET'], 'query' => $query]));
$response->statusCode(200);
return $response;
}