gpt4 book ai didi

view - 如何在 Laravel 中渲染 View 并将内容保存在文件中

转载 作者:行者123 更新时间:2023-12-04 10:39:22 25 4
gpt4 key购买 nike

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

我想创建一个 View ,获取字符串内容并将其保存到一个 xml 文件中

这是我尝试的示例代码,但它将 xml View 输出到浏览器

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 也在内部调用渲染函数。我在这里错过了什么吗?

编辑 :这是我的完整导出 Controller :我想将练习从数据库导出到独立的 html 和 XML 文件。 Hickup 在我循环练习的底部。 View 被输出到浏览器,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));
}

关于view - 如何在 Laravel 中渲染 View 并将内容保存在文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19251190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com