在我看来,我很难尝试一些非常微不足道的事情。
我想制作一个视图,获取字符串内容并将其保存到xml文件中
这是我尝试的示例代码,但它将 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);
}
}
我在 View API 中找不到任何其他方法来获取内容,__toString 也在内部调用了 render 函数。我在这里错过了什么吗?
编辑:这是我的完整导出控制器:我想将练习从数据库导出到独立的 html 和 XML 文件。小嗝嗝位于我循环练习的底部。视图输出到浏览器并且 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));
}
}
}
要获取并保存您的 html,您只需:
function getIndex(){
$fs = new Filesystem();
$data = array();
$fs->put("exercise.xml", View::make('xml', $data));
}
我发现问题了,模板文件顶部有一行字:
<?php header('Content-type: text/xml'); ?>
这导致脚本在没有请求的情况下输出到浏览器。 该模板是其他人写的,所以我不知道,但我肯定应该在询问之前检查一下