本文整理汇总了PHP中Symfony\Component\HttpFoundation\RedirectResponse类的典型用法代码示例。如果您正苦于以下问题:PHP RedirectResponse类的具体用法?PHP RedirectResponse怎么用?PHP RedirectResponse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RedirectResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: listener
public static function listener(RequestEvent $requestEvent)
{
$request = $requestEvent->getRequest();
$pathInfo = $request->getPathInfo();
$session = $requestEvent->getSession();
$configSecurity = $requestEvent->getSecurityConfig();
$routes = $requestEvent->getRoutes();
$context = new RequestContext();
$matcher = new UrlMatcher($routes, $context);
$context->fromRequest($request);
$matching = $matcher->match($pathInfo);
$matchedRoute = $matching['_route'];
if (isset($configSecurity['protected'])) {
$protected = $configSecurity['protected'];
$protectedRoutes = $protected['routes'];
$sessionKey = $protected['session'];
$notLoggedRoute = $protected['not_logged'];
$loggedRoute = $protected['logged'];
$redirectRoute = null;
if ($session->get($sessionKey) && $matchedRoute === $notLoggedRoute) {
$redirectRoute = $routes->get($loggedRoute);
}
if (!$session->get($sessionKey) && in_array($matchedRoute, $protectedRoutes)) {
$redirectRoute = $routes->get($notLoggedRoute);
}
if ($redirectRoute) {
$redirectResponse = new RedirectResponse($request->getBaseUrl() . $redirectRoute->getPath());
$redirectResponse->send();
}
}
}
示例2: match
/**
* Tries to match a URL path with a set of routes.
*
* If the matcher can not find information, it must throw one of the
* exceptions documented below.
*
* @param string $pathinfo The path info to be parsed (raw format, i.e. not
* urldecoded)
*
* @return array An array of parameters
*
* @throws ResourceNotFoundException If the resource could not be found
* @throws MethodNotAllowedException If the resource was found but the
* request method is not allowed
*/
public function match($pathinfo)
{
$urlMatcher = new UrlMatcher($this->routeCollection, $this->getContext());
$result = $urlMatcher->match($pathinfo);
if (!empty($result)) {
try {
// The route matches, now check if it actually exists
$result['content'] = $this->contentManager->findActiveBySlug($result['slug']);
} catch (NoResultException $e) {
try {
//is it directory index
if (substr($result['slug'], -1) == '/' || $result['slug'] == '') {
$result['content'] = $this->contentManager->findActiveBySlug($result['slug'] . 'index');
} else {
if ($this->contentManager->findActiveBySlug($result['slug'] . '/index')) {
$redirect = new RedirectResponse($this->request->getBaseUrl() . "/" . $result['slug'] . '/');
$redirect->sendHeaders();
exit;
}
}
} catch (NoResultException $ex) {
try {
$result['content'] = $this->contentManager->findActiveByAlias($result['slug']);
} catch (NoResultException $ex) {
throw new ResourceNotFoundException('No page found for slug ' . $pathinfo);
}
}
}
}
return $result;
}
示例3: redirectRouteAction
/**
* Returns a 301/302 redirect response based on content parameters.
*
* @param \Raindrop\RoutingBundle\Routing\Base\ExternalRouteInterface $content
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function redirectRouteAction($content)
{
$routeParams = $this->get('request')->query->all();
// do not lose eventual get parameters
if ($content instanceof ExternalRouteInterface) {
/**
* External redirect
*/
$http_status = $content->getPermanent() ? 301 : 302;
$uri = $content->getUri();
if (count($routeParams)) {
$uri .= (strpos($uri, '?') === false ? '?' : '&') . http_build_query($routeParams);
}
} else {
/**
* Inner redirect
*/
$current_route = $this->get('router')->getRouteCollection()->get($this->getRequest()->get('_route'));
$http_status = $current_route->getPermanent() ? 301 : 302;
$uri = $this->get('router')->generate($content->getName(), $routeParams, true);
}
$response = new RedirectResponse($uri, $http_status);
$response->setVary('accept-language');
return $response;
}
示例4: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$request = $event->getRequest();
if ('' !== rtrim($request->getPathInfo(), '/')) {
return;
}
$ex = $event->getException();
if (!$ex instanceof NotFoundHttpException || !$ex->getPrevious() instanceof ResourceNotFoundException) {
return;
}
$locale = $this->localeResolver->resolveLocale($request, $this->locales) ?: $this->defaultLocale;
$request->setLocale($locale);
$params = $request->query->all();
unset($params['hl']);
$response = new RedirectResponse($request->getBaseUrl() . '/' . $locale . '/' . ($params ? '?' . http_build_query($params) : ''));
$response->setPrivate();
$response->setMaxAge(0);
$response->setSharedMaxAge(0);
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('no-store', true);
$event->setResponse($response);
}
示例5: saveAction
/**
* @Route("/admin/asset/location-type/save")
* @Method("POST")
*/
public function saveAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$response = new Response();
$locationTypes = [];
$locationTypes['types'] = $em->getRepository('AppBundle\\Entity\\Asset\\LocationType')->findAll();
$form = $this->createForm(LocationTypesType::class, $locationTypes, ['allow_extra_fields' => true]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$locationTypes = $form->getData();
foreach ($locationTypes['types'] as $type) {
$em->persist($type);
}
$em->flush();
$this->addFlash('notice', 'common.success');
$response = new RedirectResponse($this->generateUrl('app_admin_asset_locationtype_index', [], UrlGeneratorInterface::ABSOLUTE_URL));
$response->prepare($request);
return $response->send();
} else {
$errorMessages = [];
$formData = $form->all();
foreach ($formData as $name => $item) {
if (!$item->isValid()) {
$errorMessages[] = $name . ' - ' . $item->getErrors(true);
}
}
return $this->render('admin/asset/location_types.html.twig', array('location_types_form' => $form->createView(), 'base_dir' => realpath($this->container->getParameter('kernel.root_dir') . '/..')));
}
}
示例6: themeAction
/**
* Переключение активной темы
* @Route("/theme/{theme}", name="theme", defaults={"theme"="web"}, requirements={"theme"="web|phone"})
*/
public function themeAction(Request $request, $theme)
{
$referer = $request->headers->get('referer');
$response = new RedirectResponse($referer);
$cookie = new Cookie('theme', $theme, 0, '/', null, false, false);
$response->headers->setCookie($cookie);
$response->send();
}
示例7: run
public function run()
{
$user = $this->getUser();
if (empty($user)) {
$response = new RedirectResponse($this->getApp()->generateUrl('home'));
$response->send();
exit;
}
}
示例8: processCallback
/**
* Handle the callback.
*
* @param OrderInterface $order
* @param string $action
*
* @return Response
*/
public function processCallback(OrderInterface $order, $action)
{
$params = array('bank' => $this->getCode(), 'reference' => $order->getReference(), 'check' => $this->generateUrlCheck($order), 'action' => $action);
$url = $this->router->generate($this->getOption('url_callback'), $params, true);
$response = $this->browser->get($url);
$routeName = 'ok' === $response->getContent() ? 'url_return_ok' : 'url_return_ko';
$response = new RedirectResponse($this->router->generate($this->getOption($routeName), $params, true), 302, array('Content-Type' => 'text/plain'));
$response->setPrivate();
return $response;
}
示例9: canBeRedirected
protected function canBeRedirected(Request $request, RedirectResponse $response)
{
$targetRequest = Request::create($response->getTargetUrl());
$stripUrl = function ($path) {
return preg_replace('/#.+$/', '', $path);
};
$targetPath = $stripUrl($targetRequest->getBaseUrl() . $targetRequest->getPathInfo());
$currentPath = $stripUrl($request->getBaseUrl() . $request->getPathInfo());
return $targetPath !== $currentPath;
}
示例10: authorize
public function authorize()
{
if ($this->request->has('code')) {
$state = $this->request->get('state');
// This was a callback request from linkedin, get the token
return $this->wunderlistService->requestAccessToken($this->request->get('code'), $state);
} else {
$url = $this->wunderlistService->getAuthorizationUri();
$response = new RedirectResponse($url);
$response->send();
}
}
示例11: submitForm
public function submitForm(array &$form, FormStateInterface $form_state)
{
if (isset($form)) {
if (isset($form['#token'])) {
$collection_name = Html::escape($form['searchblox_collection']['#value']);
\Drupal::state()->set('searchblox_collection', $collection_name);
$response = new RedirectResponse(\Drupal::url('searchblox.step3'));
$response->send();
return;
}
}
}
示例12: 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();
}
}
示例13: exec
public static function exec($url, $status = 302, $cookies = array())
{
trigger_error('deprecated since version 2.1 and will be removed in 2.3. A response can not be send before the end of the script. Please use RedirectResponse directly', E_USER_DEPRECATED);
if (false == Tlog::getInstance()->showRedirect($url)) {
$response = new RedirectResponse($url, $status);
foreach ($cookies as $cookie) {
if (!$cookie instanceof Cookie) {
throw new \InvalidArgumentException(sprintf('Third parameter is not a valid Cookie object.'));
}
$response->headers->setCookie($cookie);
}
$response->send();
}
}
示例14: indexAction
/**
* @Route("/{code}", name="short_url")
*/
public function indexAction(Request $request, $code)
{
$shortCode = $this->get('short_url')->get($code);
if ($shortCode) {
$response = new RedirectResponse($shortCode->getUrl(), RedirectResponse::HTTP_FOUND);
$response->setPublic();
$response->setExpires(new DateTime('+1 year'));
return $response;
}
$response = new Response();
$response->setPublic();
$response->setExpires(new DateTime('+1 hour'));
return $this->render('notFound.html.twig', ['code' => $code], $response);
}
示例15: create
public function create($request, $response)
{
$formFactory = Forms::createFormFactory();
/* @var $form \Symfony\Component\Form\Form */
$form = $formFactory->create(new BookType());
$form->handleRequest();
if ($form->isValid()) {
$this->em->persist($form->getData());
$this->em->flush();
$this->session->getFlashBag()->add('notice', 'Book inserted');
$response = new RedirectResponse('/book');
return $response;
}
return $response->create($this->template->render('book/create.html.twig', ['title' => 'New Book', 'form' => $form->createView()]));
}