Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用laravel,如何创建动态文件下载?_Php_Laravel_Http Headers_Download - Fatal编程技术网

Php 使用laravel,如何创建动态文件下载?

Php 使用laravel,如何创建动态文件下载?,php,laravel,http-headers,download,Php,Laravel,Http Headers,Download,我正在用Laravel将我当前的一个站点转换为MVC模式,我一直在研究如何管理一个功能:动态日历下载 当前,当用户导航到calendar.php?var=1时,将准备下载calendar.ics文件,并自动提示用户: header('Content-type: text/calendar; charset=utf-8'); header('Content-Disposition: inline; filename='.$calData->filename($calData->file

我正在用Laravel将我当前的一个站点转换为MVC模式,我一直在研究如何管理一个功能:动态日历下载

当前,当用户导航到
calendar.php?var=1
时,将准备下载
calendar.ics
文件,并自动提示用户:

header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename='.$calData->filename($calData->filename).'.ics'); 


$calData = new CalendarBuilder($_GET['foo']);

$ical = "BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";

        $ical .= "BEGIN:VEVENT
UID:" . md5(uniqid(mt_rand(), true)) . "@foo.com
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:".$calData->eventStart($calData->foobar)."
DTEND:".$calData->eventEnd($calData->foobaz)."
SUMMARY:".$calData->summary($calData->fooboo)."
LOCATION:".$calData->location($calData->foofaa)."
DESCRIPTION:".$calData->description($calData->fi)."
END:VEVENT\r\n";

$ical .= "END:VCALENDAR";

echo $ical;
exit;
但是,现在我希望用户能够导航到
calendar/{slug}
,并立即发送下载响应。然而,使用Laravel,我看不到任何方法可以做到这一点。例如,我可以设置一条路线:

Route::get('calendar/{slug}', array(
    'as' => 'calendar.get',
    'uses' => 'CalendarController@get'
));
这可能会转到我的控制器:

class CalendarController extends BaseController {

    // GET Specific Calendar
    public class get() {
        // some logic
        return Response::download();
    }
}
但是使用
Response::download()
方法,似乎没有任何方法可以动态生成文件,因为该方法的参数是:

Response::download($pathToFile, $name, $headers);

它似乎需要一个静态的、预先存在的文件。在使用Laravel的MVC架构时,实现我目前拥有的功能的最佳方式是什么

我只是在做类似的事情

以下是它对我的作用:

return Response::make($ical)->header("Content-type","text/calendar; charset=utf-8")->header("Content-disposition","attachment; filename=\"".$calData->filename($calData->filename)."\"");
我知道现在有点晚了,但这里应该有一个答案供其他人寻找