Laravel 路由:如何创建指向 PDF 文件的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18640618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:23:06  来源:igfitidea点击:

Laravel routing: How to create a link to a PDF file

phplaravellaravel-4

提问by user2746374

I need to generate PDF documents and have them send by email as well as make them reachable by a link. Sending by email is OK, but I have no idea how to create the link.

我需要生成 PDF 文档并让它们通过电子邮件发送以及通过链接访问它们。通过电子邮件发送可以,但我不知道如何创建链接。

Since I use Laravel 4, all links are going through routes.php. So how do I create a link to a file at a given directory?

由于我使用 Laravel 4,所有链接都通过routes.php. 那么如何在给定目录中创建指向文件的链接?

<a href="http://my.domain.org/assets/pdf/file.pdf">Link</a> 

will not work, since the route is not known by laravel...

将不起作用,因为 laravel 不知道该路线...

回答by Aken Roberts

To generate a link to a physical file or directory, that you don't want to run through Laravel's application (e.g. through index.php), use URL::asset().

要生成指向物理文件或目录的链接,您不想通过 Laravel 的应用程序(例如通过 index.php)运行,请使用URL::asset().

URL::asset('assets/pdf/file.pdf');

This assumes you've created a physical PDF file on your server that you want to link to. If your PDF file is dynamic and generated/retrieved through your Laravel application, you should use URL::to(), URL::route(), or similar.

这假设您已经在要链接到的服务器上创建了一个物理 PDF 文件。如果你的PDF文件是动态的,并通过您Laravel应用程序生成/检索到的,你应该使用URL::to()URL::route()或类似的。

回答by Antonio Carlos Ribeiro

Martin Tale is right, but this is another way of accomplishing the same using Laravel's Blade:

Martin Tale 是对的,但这是使用 Laravel's Blade 完成相同任务的另一种方式:

{{ link_to('/assets/pdf/file.pdf', 'Link') }} 

回答by Martin Tale

Create that PDF somewhere in public directory and then create a link to that file like this:

在公共目录中的某处创建该 PDF,然后创建指向该文件的链接,如下所示:

URL::to('path/to/public/directory/file.pdf')

回答by Lynx

if you are using laravel 4's MAIL feature you can add this to it

如果您使用的是 laravel 4 的 MAIL 功能,您可以将其添加到其中

$message->attach($pathToFile);

but the .pdf in your public directory under a pdf folder and that will make it:

但是 pdf 文件夹下公共目录中的 .pdf 将使它:

$message->attach('pdf/mypdffile.pdf');

the same can be for a link which you can add to the email

您可以添加到电子邮件中的链接也是如此

{{ URL::to('pdf/mypdffile.pdf') }}

You shouldn't need a route to create a link it can be done by blade.

您不需要路由来创建可以由刀片完成的链接。

回答by entoniperez

Another way, in a view:

另一种方式,在一个视图中:

<a href="{{ asset('/your_pdf_file.pdf') }}">some text</a>

This link will go to publicfolder.

此链接将转到public文件夹。