gpt4 book ai didi

Symfony 重定向到外部 URL

转载 作者:行者123 更新时间:2023-12-03 12:24:21 25 4
gpt4 key购买 nike

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

我试过这个选项:

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

错误:找不到“GET/www.example.com”的路由
2- $this->redirect("www.example.com");

错误: Controller 必须返回响应(给出空值)。
3- $response = new Response();
$response->headers->set("Location","www.example.com");
return $response

没有错误但空白页!

最佳答案

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

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 格式。

关于Symfony 重定向到外部 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29747531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com