2010-01-13 73 views
1
自定义URL

使用的Symfony 1.0,是有可能创建一个链接到一个静态文件即PDF或DOC文件,并链接到它的自定义URL添加一个链接到一个静态文件上的Symfony

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

出现这种情况的原因是一些新的网站开发前的印刷文档指向指定的网址。

回答

2

好吧,我想通了(从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; 
    } 
+0

很高兴看到博客文章是有帮助的:) – Timo 2011-09-26 07:51:46