本文整理汇总了PHP中Response::setStatusCode方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::setStatusCode方法的具体用法?PHP Response::setStatusCode怎么用?PHP Response::setStatusCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Response
的用法示例。
在下文中一共展示了Response::setStatusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parse
/**
* Parses a raw response string, including headers and body, and returns a Response object.
*
* @param string $response
*
* @return \Brick\Http\Response
*
* @throws \RuntimeException
*/
public static function parse($response)
{
$responseObject = new Response();
if (preg_match('/^HTTP\\/([0-9]\\.[0-9]) ([0-9]{3}) .*\\r\\n/', $response, $matches) == 0) {
throw new \RuntimeException('Could not parse response (error 1).');
}
list($line, $protocolVersion, $statusCode) = $matches;
$responseObject->setProtocolVersion($protocolVersion);
$responseObject->setStatusCode($statusCode);
$response = substr($response, strlen($line));
for (;;) {
$pos = strpos($response, Message::CRLF);
if ($pos === false) {
throw new \RuntimeException('Could not parse response (error 2).');
}
if ($pos == 0) {
break;
}
$header = substr($response, 0, $pos);
if (preg_match('/^(\\S+):\\s*(.*)$/', $header, $matches) == 0) {
throw new \RuntimeException('Could not parse response (error 3).');
}
list($line, $name, $value) = $matches;
if (strtolower($name) == 'set-cookie') {
$responseObject->setCookie(Cookie::parse($value));
} else {
$responseObject->addHeader($name, $value);
}
$response = substr($response, strlen($line) + 2);
}
$body = substr($response, 2);
$responseObject->setContent($body);
return $responseObject;
}
示例2: invalidate
protected function invalidate(Request $request, $catch = false)
{
if ('PURGE' !== $request->getMethod()) {
return parent::invalidate($request, $catch);
}
if ('127.0.0.1' !== $request->getClientIp()) {
return new Response('Invalid HTTP method', Response::HTTP_BAD_REQUEST);
}
$response = new Response();
if ($this->getStore()->purge($request->getUri())) {
$response->setStatusCode(200, 'Purged');
} else {
$response->setStatusCode(200, 'Not found');
}
return $response;
}
示例3: redirect
function redirect($url)
{
$response = new Response();
$completeURL = url($url);
$response->addHeader('HTTP/1.1 302 Found');
$response->addHeader("Location: " . $completeURL);
$response->setStatusCode(302);
return $response;
}
示例4: responseFromOAuth
/**
* Creates Phalcon response from OAuth2 Response.
*
* @param $oauth_response
* @return Response
*/
public static function responseFromOAuth($oauth_response)
{
$response = new Response();
$response->setStatusCode($oauth_response->getStatusCode());
foreach ($oauth_response->getHttpHeaders() as $header) {
$response->setRawHeader($header);
}
$response->setContent($oauth_response->getResponseBody());
return $response;
}
示例5: get
protected function get($path, array $query = array())
{
$response = null;
try {
$response = parent::get($path, $query);
} catch (\Exception $e) {
$http_response = new Response();
$http_response->setStatusCode(Response::STATUS_CODE_500);
$response = new ZendTwitterResponse($http_response);
}
return $response;
}
示例6: error
/**
* show error page
*
* call error action method and set response status code
* This will work as well for ajax call, see how ajax calls are handled in main.js
*
* @param int|string $code
*
*/
public function error($code)
{
$errors = [404 => "notfound", 401 => "unauthenticated", 403 => "unauthorized", 400 => "badrequest", 500 => "system"];
if (!isset($errors[$code]) || !method_exists("ErrorsController", $errors[$code])) {
$code = 500;
}
$action = isset($errors[$code]) ? $errors[$code] : "System";
$this->response->setStatusCode($code);
// clear, get page, then send headers
$this->response->clearBuffer();
(new ErrorsController($this->request, $this->response))->{$action}();
return $this->response;
}
示例7: error
/**
* show error page
*
* call error action method and set response status code
* This will work as well for ajax call, see how ajax calls are handled in main.js
*
* @param string $error
*
*/
public function error($error)
{
$errorController = new ErrorsController();
if (!method_exists("ErrorsController", $error)) {
$error = "System";
}
$errors = ["notfound" => 404, "system" => 500, "badrequest" => 400, "unauthorized" => 401, "forbidden" => 403];
$code = isset($errors[strtolower($error)]) ? $errors[strtolower($error)] : 500;
$this->response->setStatusCode($code);
//clear, get page, then send headers
$this->response->clearBuffer();
$errorController->{$error}();
$this->response->send();
}
示例8: outputError
public static function outputError($exception)
{
if (PHP_SAPI == 'cli') {
echo $exception->getMessage() . "\nFile: " . $exception->getFile() . ' on line ' . $exception->getLine() . "\n\n" . $exception->getTraceAsString() . "\n";
// exit with an error code
exit(1);
}
// Write the error to log file
@error_log('Error 404 Page Not Found: ' . $_SERVER['REQUEST_URI']);
$response = new Response();
$response->setStatusCode(404);
$response->setBody(Controller::outputError('errors/404', ['message' => $exception->getMessage()]));
return $response;
}
示例9: outputError
public static function outputError($message = null, $file = false, $line = false, $trace = false)
{
// Message for log
$errorMessage = 'Error ' . $message . ' in ' . $file . ' line: ' . $line;
// Write the error to log file
@error_log($errorMessage);
// Just output the error if the error source for view file or if in cli mode.
if (PHP_SAPI == 'cli') {
exit($errorMessage);
}
$code = [];
if (!$file) {
goto constructViewData;
}
$fileString = file_get_contents($file);
$arrLine = explode("\n", $fileString);
$totalLine = count($arrLine);
$getLine = array_combine(range(1, $totalLine), array_values($arrLine));
$startIterate = $line - 5;
$endIterate = $line + 5;
if ($startIterate < 1) {
$startIterate = 1;
}
if ($endIterate > $totalLine) {
$endIterate = $totalLine;
}
for ($i = $startIterate; $i <= $endIterate; $i++) {
$html = '<span style="margin-right:10px;background:#CFCFCF;">' . $i . '</span>';
if ($line == $i) {
$html .= '<span style="color:#DD0000">' . htmlentities($getLine[$i]) . "</span>\n";
} else {
$html .= htmlentities($getLine[$i]) . "\n";
}
$code[] = $html;
}
constructViewData:
$data = ['message' => $message, 'file' => $file, 'line' => $line, 'code' => $code, 'trace' => $trace];
$response = new Response();
$response->setStatusCode(500);
$response->setBody(Controller::outputError('errors/500', $data));
return $response;
}
示例10: supprimerfraishorsforfaitAction
public function supprimerfraishorsforfaitAction($id)
{
$session = $this->get('request')->getSession();
$idVisiteur = $session->get('id');
$mois = getMois(date("d/m/Y"));
$numAnnee = substr($mois, 0, 4);
$numMois = substr($mois, 4, 2);
$pdo = PdoGsb::getPdoGsb();
if ($pdo->estValideSuppressionFrais($idVisiteur, $mois, $id)) {
$pdo->supprimerFraisHorsForfait($id);
} else {
$response = new Response();
$response->setContent("<h2>Page introuvable erreur 404 ");
$response->setStatusCode(404);
return $response;
}
$lesFraisForfait = $pdo->getLesFraisFrofait($idVisiteur, $mois);
$lesFraisHorsForfait = $pdo->getLesFraisHorsForfait($idVisiteur, $mois);
return $this->render('PgGsbFraisBundle:SaisirFrais:saisirtouslesfrais.html.twig', array('lesfraisforfait' => $lesFraisForfait, 'lesfraishorsforfait' => $lesFraisHorsForfait, 'nummois' => $numMois, 'numannee' => $numAnnee, 'leserreursforfait' => null, 'leserreurshorsforfait' => null));
}
示例11: testSetStatusThrowInvalidArgumentWhenStatusCodeArentInHttpFoundation
/**
* @expectedException \InvalidArgumentException
* @dataProvider invalidStatusCodesProvider
*/
public function testSetStatusThrowInvalidArgumentWhenStatusCodeArentInHttpFoundation($invalidStatusCode)
{
$response = new Response();
$response->setStatusCode($invalidStatusCode);
}
示例12: route
/**
* @param Huxtable\Web\Request $request
* @return Huxtable\Web\Response
*/
public function route(Request $request)
{
$url = $request->getURL();
$method = $request->getMethod();
// Default response
$response = new Response();
$response->setStatusCode(404);
$response->setContents('Not Found :(');
if (isset($this->routes[$url])) {
$routes = $this->routes[$url];
// Route pattern + method match found
if (isset($this->routes[$url][$method])) {
$routeObject = $this->routes[$url][$method];
$routeObject->setRequest($request);
// Call authenticatiion callback if defined
$authenticationClosure = $routeObject->getAuthenticationClosure();
if ($authenticationClosure != false) {
try {
call_user_func($authenticationClosure);
} catch (Request\UnauthorizedException $e) {
$routeObject->response->setStatusCode(401);
$routeObject->response->setContents($routeObject->response->getStatusMessage());
return $routeObject->response;
}
}
// Confirm required headers were sent
foreach ($routeObject->getRequiredHeaders() as $header) {
if ($request->getHeader($header) === false) {
$response->setStatusCode(400);
$response->setContents($routeObject->response->getContents());
return $response;
}
}
// Confirm required arguments were passed
foreach ($routeObject->getRequiredArguments() as $argument) {
if ($request->getArgument($argument) === false) {
$response = new Response();
$response->setStatusCode(400);
$response->setContents("Missing required argument '{$argument}'");
return $response;
}
}
try {
// Call the main routing closure
$contents = call_user_func($routeObject->getClosure());
} catch (\Exception $e) {
$response = new Response();
$response->setStatusCode(500);
$response->setContents($e->getMessage());
return $response;
}
$routeObject->response->setContents($contents);
return $routeObject->response;
}
// No route pattern + method match found
$response->setStatusCode(405);
$response->setContents('Method Not Allowed :(');
}
return $response;
}
示例13: array
} else {
// Change the HTTP status
$response->setStatusCode(409, "Conflict");
$errors = array();
foreach ($status->getMessages() as $message) {
$errors[] = $message->getMessage();
}
$response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors));
}
return $response;
});
// Deletes robots based on primary key
$app->delete('/api/robots/{id:[0-9]+}', function ($id) use($app) {
$phql = "DELETE FROM Robots WHERE id = :id:";
$status = $app->modelsManager->executeQuery($phql, array('id' => $id));
// Create a response
$response = new Response();
if ($status->success() == true) {
$response->setJsonContent(array('status' => 'OK'));
} else {
// Change the HTTP status
$response->setStatusCode(409, "Conflict");
$errors = array();
foreach ($status->getMessages() as $message) {
$errors[] = $message->getMessage();
}
$response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors));
}
return $response;
});
$app->handle();
示例14: handle
/**
* Handles the authentication for user.
*
* @param GetResponseEvent $event The response event.
*
* @throws AuthenticationException When the request is not authenticated.
*
* @return void
*/
public function handle(GetResponseEvent $event)
{
$request = $event->getRequest();
$wsseRegex = '/UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([^"]+)", Created="([^"]+)"/';
if (!$request->headers->has('x-wsse') || 1 !== preg_match($wsseRegex, $request->headers->get('x-wsse'), $matches)) {
return;
}
$token = new WsseUserToken($this->providerKey);
$token->setUser($matches[1]);
$token->setDigest($matches[2]);
$token->setNonce($matches[3]);
$token->setCreated($matches[4]);
try {
$authToken = $this->authenticationManager->authenticate($token);
$this->securityContext->setToken($authToken);
return;
} catch (AuthenticationException $failed) {
$failedMessage = 'WSSE Login failed for ' . $token->getUsername() . '. Because: ' . $failed->getMessage();
$token = $this->securityContext->getToken();
if ($token instanceof WsseUserToken && $this->providerKey === $token->getProviderKey()) {
$this->securityContext->setToken(null);
}
// deny authentication with a '403 Forbidden' HTTP response
$response = new Response();
$response->setStatusCode(403);
$event->setResponse($response);
return;
}
// by default deny authorization
$response = new Response();
$response->setStatusCode(403);
$event->setResponse($response);
}
示例15: getNotFoundResponse
/**
* Creates default Response in case of emergency.
*
* @param $message
*
* @return Response
*/
private function getNotFoundResponse($message)
{
$response = new Response($message);
$response->setStatusCode(404);
return $response;
}