【问题标题】:Adding a link to a static file with a custom URL on Symfony在 Symfony 上使用自定义 URL 添加指向静态文件的链接
【发布时间】:2010-01-13 11:26:18
【问题描述】:

使用 Symfony 1.0 时,是否可以创建指向静态文件的链接,即 PDF 或 DOC 文件并链接到自定义 URL

我想添加一个文件,例如example.pdf 到服务器上的 web/uploads/pdf 文件夹,然后使用 URL 链接到它,例如:http://www.example.com/docs/old/test/example.pdf

出现这种情况是因为在开发新网站之前的一些印刷文档指向了指定的 URL。

【问题讨论】:

    标签: model-view-controller symfony1 url-routing


    【解决方案1】:

    好的,我已经想通了(在this blog post 的大力帮助下。感谢Thaberkern

    创建获取文件名的路由:

    legacy_docs:
      url: /docs/:filename.pdf
      param: { module: default, action: display_pdf }
    

    然后创建一个动作来输出pdf:

      public function executeDisplayPdf()
      {
        $filename = $this->getRequestParameter('filename');
    
        $path_to_pdf = 'http://'.sfConfig::get('website_hostname').'/uploads/'.'/'.$filename.'.pdf';
    
        //create the response object
        $this->getResponse()->clearHttpHeaders();
        $this->getResponse()->setHttpHeader('Pragma: public', true);
        $this->getResponse()->setContentType('application/pdf');
        $this->getResponse()->sendHttpHeaders();
        $this->getResponse()->setContent(readfile($path_to_pdf));
    
        //set up the view
        $this->setLayout(false);
        sfConfig::set('sf_web_debug', false);
    
        return sfView::NONE;
      }
    

    【讨论】:

    • 很高兴看到这篇博文很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 2020-01-19
    • 2010-12-02
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多