本文整理汇总了PHP中Symfony\Component\HttpFoundation\RedirectResponse::setStatusCode方法的典型用法代码示例。如果您正苦于以下问题:PHP RedirectResponse::setStatusCode方法的具体用法?PHP RedirectResponse::setStatusCode怎么用?PHP RedirectResponse::setStatusCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpFoundation\RedirectResponse
的用法示例。
在下文中一共展示了RedirectResponse::setStatusCode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* Handles an access denied failure redirecting to home page
*
* @param Request $request
* @param AccessDeniedException $accessDeniedException
*
* @return Response may return null
*/
public function handle(Request $request, AccessDeniedException $accessDeniedException)
{
$this->logger->error('User tried to access: ' . $request->getUri());
if ($request->isXmlHttpRequest()) {
return new JsonResponse(['message' => $accessDeniedException->getMessage(), 'trace' => $accessDeniedException->getTraceAsString(), 'exception' => get_class($accessDeniedException)], Response::HTTP_SERVICE_UNAVAILABLE);
} else {
$url = $request->getBasePath() !== "" ? $request->getBasePath() : "/";
$response = new RedirectResponse($url);
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->prepare($request);
return $response->send();
}
}
示例2: realpath
/* Ensure requested file is actually in the content folder.
* You'd think I'd do this sooner, but we don't know the actual
* filename before this point.
*/
$filename = realpath($filename);
if (strpos($filename, realpath(__DIR__ . '/../content')) !== 0) {
// Attempt to read file outside of content folder
$filename = __DIR__ . '/../content/404.html';
}
/* Render the file:
* Dispatch a FileCopyEvent to call transformers on a file, then build a response
*/
$event = new FileCopyEvent($filename, $filename);
$extension = $event->getExtension();
$event = $container->get('event_dispatcher')->dispatch(FilesystemEvents::COPY, $event);
$response = new Response($event->getContent(), 200);
$headers = $event->data->get('headers')->getOrElse(array());
// Special cases
if (isset($headers['location'])) {
$response = new RedirectResponse($headers['location']);
unset($headers['location']);
}
if (isset($headers['status'])) {
$response->setStatusCode($headers['status']);
unset($headers['status']);
}
// Everything else
foreach ($headers as $key => $value) {
$response->headers->set($key, $value);
}
$response->send();