2010-01-13 138 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