Как визуализировать представление и сохранить содержимое в файле в LaravelPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как визуализировать представление и сохранить содержимое в файле в Laravel

Сообщение Anonymous »

На мой взгляд, мне сложно попробовать что-то очень тривиальное.

Я хочу создать представление, получить содержимое строки и сохранить его в 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 файлы. Задержка находится внизу, где я повторяю упражнения. Представление выводится в браузер, и 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 . "
";
$fs->copyDirectory($playerPath, $path);

//copy resources
echo "copying resources " . $path . "
";
$fs->copyDirectory($uploadsPath . $exercise->id . "/", $path);

//save xml file
$content = Exercise::getXmlContent($exercise->id);
$fs->put($path . "exercise.xml", View::make('xml', $content));
}
}

}


Подробнее здесь: https://stackoverflow.com/questions/192 ... in-laravel
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»