如何渲染视图并将内容保存在Laravel中的文件中

时间:2013-10-08 14:55:39

标签: view laravel laravel-4 output

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

我想创建一个视图,获取字符串内容并将其保存到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文件。 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));
        }
    }

}

2 个答案:

答案 0 :(得分:11)

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

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

    $data = array();

    $fs->put("exercise.xml", View::make('xml', $data));
}

答案 1 :(得分:1)

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

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

这导致脚本输出到浏览器,而不是要求它。 该模板是由其他人编写的,所以我不知道它,但我肯定应该在询问之前检查它