Symfony 重定向到外部 URL [英] Symfony redirect to external URL

查看:27
本文介绍了Symfony 重定向到外部 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 symfony 操作中重定向到外部 URL?

How can I redirect to an external URL within a symfony action?

我试过这个选项:

1- return $this->redirect("www.example.com");

错误:找不到GET/www.example.com"的路由

Error : No route found for "GET /www.example.com"

2- $this->redirect("www.example.com");

错误:控制器必须返回响应(给定 null).

Error : The controller must return a response (null given).

3- $response = new Response();
$response->headers->set("Location","www.example.com");
return $response

没有错误但空白页!

推荐答案

您的问题的答案在官方 Symfony 书籍中.

Answer to your question is in official Symfony book.

http://symfony.com/doc/current/book/controller.html#redirecting

public function indexAction()
{
    return $this->redirect('http://stackoverflow.com');
    // return $this->redirect('http://stackoverflow.com', 301); - for changing HTTP status code from 302 Found to 301 Moved Permanently
}

什么是URL"?你真的为这种模式定义了路线吗?如果不是,那么 not found 错误是绝对正确的.如果要重定向到外部站点,请始终使用绝对 URL 格式.

What is the "URL"? Do you have really defined route for this pattern? If not, then not found error is absolutelly correct. If you want to redirect to external site, always use absolute URL format.

这篇关于Symfony 重定向到外部 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆