在我有多个视图的情况下,在循环中我应该将数据传递给那些视图并加载它,之后我需要用DOMDocument()
解析加载的视图并将它们写入excel的特定单元格。我试过下面的步骤,但有问题。
foreach($data as $result_row){
$result_path = Config::get('auditConfig.result_page.'.$result_row['_type']);
$view = View::make($result_path, array('item' => $result_row));
$content = $view->render();
$doc = new \DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadHTMLFile($content);
$table_tr = $doc->getElementsByTagName('tr');
foreach($table_tr as $n) {
echo $n->nodeValue;
}
}
答案 0 :(得分:2)
正如您可以看到函数loadHTMLFile
public bool DOMDocument :: loadHTMLFile(string $ filename [,int $ options = 0])
第一个参数是$filename
,表示您要阅读的文件的路径
在您的情况下,您提供的是视图文件的内容,而不是该视图的文件路径。
假设您以users.blade.php的身份查看,那么您将提供以下路径:
$doc->loadHTMLFile(app_path('Views/Users/users.blade.php'));
OR
$viewFile = app_path('Views/Users/users.blade.php');
$doc->loadHTMLFile($viewFile);
因此,您的代码将按以下方式更改
foreach ($data as $result_row) {
$result_path = Config::get('auditConfig.result_page.' . $result_row['_type']);
//$view = View::make($result_path, array('item' => $result_row));
//$content = $view->render();
$viewFile = app_path($result_path);
$doc = new \DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadHTMLFile($viewFile);
$table_tr = $doc->getElementsByTagName('tr');
foreach ($table_tr as $n) {
echo $n->nodeValue;
}
}
以字符串形式阅读。
foreach ($data as $result_row) {
$result_path = Config::get('auditConfig.result_page.' . $result_row['_type']);
$view = View::make($result_path, array('item' => $result_row))->__toString();
//$content = $view->render();
$viewFile = app_path($result_path);
$doc = new \DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadHTML($view);
$table_tr = $doc->getElementsByTagName('tr');
foreach ($table_tr as $n) {
echo $n->nodeValue;
}
}