如何在 Laravel 中渲染视图并将内容保存在文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19251190/
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
how to render a view and save the content in a file in Laravel
提问by Ernie
I'm having a hard time trying something very trivial in my opinion.
在我看来,我很难尝试一些非常微不足道的事情。
I want to make a view, get the string contents and save it to an xml file
我想创建一个视图,获取字符串内容并将其保存到一个 xml 文件中
Here is an example code of what i try, but it outputs the xml view to the browser
这是我尝试的示例代码,但它将 xml 视图输出到浏览器
use Illuminate\Filesystem\Filesystem;
class ExportController extends BaseController {
function getIndex(){
$fs = new Filesystem();
$data = array();
$view = View::make('xml', $data);
$html = $view->render(); //outputs content to the browser
$fs->put("exercise.xml", $html);
}
}
I can't find any other method in the View API to get the contents, __toString also calls the render function internally. Am I missing something here?
我在 View API 中找不到任何其他方法来获取内容, __toString 也在内部调用渲染函数。我在这里错过了什么吗?
EDIT: this is my full export controller: i want to export exercises from a database to standalone html and XML files. The hickup is at the bottom where I loop the exercises. The view gets outputted to the browser and the foreach stops
编辑:这是我的完整导出控制器:我想将练习从数据库导出到独立的 html 和 XML 文件。Hickup 位于底部,我在那里循环练习。视图被输出到浏览器,foreach 停止
use Illuminate\Filesystem\Filesystem;
set_time_limit(0);
class ExportController extends BaseController {
function __construct(){
}
function getIndex($methodId = NULL, $exerciseId = NULL){
$base = base_path() . "/export/";
$playerPath = base_path() . "/public/preview/dist/";
$uploadsPath = base_path() . "/public/uploads/";
if($methodId ===NULL && $exerciseId === NULL){
$up = new Exception('Please provide a method-id or an exercise id');
throw $up;
}
//we make an array which holds all the exercises we have to export
$exercises = array();
//get all exercises for given methodId
if($methodId !== NULL){
$method = Method::with('categories.exercises')->find($methodId);
if($method == NULL) break;
foreach($method->categories as $category){
foreach($category->exercises as $exercise){
array_push($exercises, $exercise);
}
}
}
if($exerciseId != null){
$exercise = Exercise::find($exerciseId);
if($exercise != NULL) array_push($exercises, $exercise);
}
if(empty($exercises)){
$up = new Exception('No exercises could be found for given method/exercise');
throw $up;
}
//loop the exercises and publish like a charm
foreach($exercises as $exercise){
//determine destination
$path = $base . $exercise->getPath();
//check if path exists, if it does, wipe it out
$fs = new Filesystem();
if($fs->exists($path)){
$fs->deleteDirectory($path, true);
}
//copy player files
echo "copying " . $path . "<br />";
$fs->copyDirectory($playerPath, $path);
//copy resources
echo "copying resources " . $path . "<br />";
$fs->copyDirectory($uploadsPath . $exercise->id . "/", $path);
//save xml file
$content = Exercise::getXmlContent($exercise->id);
$fs->put($path . "exercise.xml", View::make('xml', $content));
}
}
}
回答by Antonio Carlos Ribeiro
To get and save your html you just have to:
要获取并保存您的 html,您只需:
function getIndex(){
$fs = new Filesystem();
$data = array();
$fs->put("exercise.xml", View::make('xml', $data));
}
回答by Ernie
I found the problem, on top of the template file was a line that said
我发现了问题,在模板文件的顶部是一行说
<?php header('Content-type: text/xml'); ?>
That caused the script to output to the browser, without asking for it. The template was written by someone else so I didn't know about it, but surely I should have checked it before asking
这导致脚本输出到浏览器,而不需要它。模板是别人写的,所以我不知道,但在问之前我肯定应该检查一下