Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to render a view and save the content in a file in Laravel

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

Here is an example code of what i try, but it outputs the xml view to the browser

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?

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

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));
        }
    }

}
like image 900
Ernie Avatar asked Oct 08 '13 14:10

Ernie


2 Answers

To get and save your html you just have to:

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

    $data = array();

    $fs->put("exercise.xml", View::make('xml', $data));
}
like image 168
Antonio Carlos Ribeiro Avatar answered Nov 13 '22 15:11

Antonio Carlos Ribeiro


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

like image 26
Ernie Avatar answered Nov 13 '22 15:11

Ernie