开发者

Symfony Routing Question

I am running Symfony 1.4 on Linux. My application creates pdf files and saves the files in the following directory: /srv/www/vhosts/myapp/htdocs/stmts

Here is an example of the path to a particular pdf file: /srv/www/vhost开发者_JAVA百科s/myapp/htdocs/stmts/example_001.pdf

My symfony is installed in the following path: /srv/www/vhosts/myapp/htdocs

How can I create a route from my Symfony application to the example_001.pdf file? I want to be able to create a link in my symfony application to the pdf file. When a user clicks on the link the pdf will be opened.

Thank You


In order for using a route to make sense you would need to be doing something like this:

public function executeDownload(sfWebRequest $request)
{
    // assume this method holds the logic for generating or getting a path to the pdf
    $pdfPath = $this->getOrCreatePdf();

    // disbale the layout
    $this->setLayout(false);

    $response = $this->getResponse();

    // return the binary pdf dat directly int he response as if serving a static pdf file
    $response->setHttpHeader('Content-Disposition', 'attachment; filename="'. basename($pdfPath));
    $response->setContentType('application/pdf');
    $response->setContent(file_get_contents($pdfPath));

    return sfView::NONE;
}

That action would actually read the file and send the content. But unless you have a good reason for doing this its not advisable because youre going to incur unnecessary overhead from php.

If you do have a good reason for doin this (restricted access, dynamic file names, etc.) then you would simply determine what paramters you need to use in that action to determine the path to the pdf on the file system and set up a normal route. For example lets say your using a human recognizeable slug to refernece the file. Then you have a db record that holds a mapping of slug to file path. In that case the preceding action might look like this:

public function executeDownload(sfWebRequest $request)
{

   $q = Doctrine_Core::getTable('PdfAsset')
     ->createQuery('p')
     ->where('slug = ?', $request->getSlug());

    $this->forward404Unless($asset = $q->fetchOne());

    $pdfPath = $asset->getPath();

    // disbale the layout
    $this->setLayout(false);

    $response = $this->getResponse();

    // return the binary pdf dat directly in the response as if serving a static pdf file
    $response->setHttpHeader('Content-Disposition', 'attachment; filename="'. basename($pdfPath));
    $response->setContentType('application/pdf');
    $response->setContent(file_get_contents($pdfPath));

    return sfView::NONE;
}

With the corresponding route looking something like:

pdf_asset:
  url: /download/pdf/:slug
  params: {module: yourModule, action: 'download'}

Note if the files are large you might want to use fopen instead of file_get_contents and then read the data out as a stream so that you dont have to put it all in memory. This would require you to use a view though (but you would still set layout to false to prevent the layout from wrapping your streamed data).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜