7

在我看来,我很难尝试一些非常微不足道的事情。

我想创建一个视图,获取字符串内容并将其保存到 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 也在内部调用渲染函数。我在这里错过了什么吗?

编辑:这是我的完整导出控制器:我想将练习从数据库导出到独立的 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));
        }
    }

}
4

2 回答 2

11

要获取并保存您的 html,您只需:

function getIndex(){
    $fs = new Filesystem();

    $data = array();

    $fs->put("exercise.xml", View::make('xml', $data));
}
于 2013-10-08T15:19:39.683 回答
1

我发现了问题,在模板文件的顶部有一行说

<?php header('Content-type: text/xml'); ?>

这导致脚本在不要求的情况下输出到浏览器。模板是别人写的,所以我不知道,但我应该在问之前检查一下

于 2013-10-09T08:52:12.803 回答