【问题标题】:Generate route with symfony用 symfony 生成路由
【发布时间】:2016-03-07 12:49:20
【问题描述】:

我在控制器中使用了一个动作来从网址生成路由。

url_proxy:
    path:   /proxy
    defaults: { _controller: "PublicartelAppBundle:PlayContent:proxy" }

在我的控制器中,我使用了 symfony 路由的生成器来执行以下操作:

public function proxyAction(Request $request){
    $url = $request->query->get('url');
    $generateUrl = $this->get('router')->generate('url_proxy', array('url' => $url));
    return new Response(file_get_contents($generateUrl));
}

然后,为了证明我做到了:

http://sgc.lo/public/proxy?url=http://www.google.com

但这会产生错误:

警告:file_get_contents (/public/proxy?url=http%3A//www.google.com):无法打开流:没有这样的文件或目录

我认为是因为生成了路径:/public/proxy?url=http%3A//www.google.com,应该是

sgc.lo/public/proxy?url=http%3A//www.google.com

因为我可以在生成的路由中包含前缀“sgc.lo”?

【问题讨论】:

    标签: php symfony proxy


    【解决方案1】:

    您需要明确要求提供绝对 URL。

    $this->get('router')
      ->generate('url_proxy', array('url' => $url), UrlGeneratorInterface::ABSOLUTE_URL);
    

    【讨论】:

    • 由于某种原因,url进入了循环,没有完成加载。我必须在某处添加我的绝对路径?
    • 如果您从命令行运行,您需要明确添加您的域。
    • 我误解了你的评论。我正在尝试从浏览器中输入控制器中分配了操作的路径。
    • 您正在循环,因为您在操作中请求自己的路由(因此路由调用操作,它们从调用操作的路由获取文件内容......)。您应该将您的回复更改为 new Response(file_get_contents($url)),以便它从提供的 URL 而不是您自己的 URL 获取内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多