DOMPDF loadView()错误-未定义的变量:数据 [英] DOMPDF loadView() error - undefined variable: data

查看:98
本文介绍了DOMPDF loadView()错误-未定义的变量:数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将用于Laravel的DOMPDF包装器合并到我的项目中,但是我麻烦解决如何将变量传递到PDF模板中.

I'm currently trying to incorporate the DOMPDF Wrapper for Laravel into my project, however I'm having troble figuring out how to pass a variable into the PDF template.

按照说明,在我的控制器中,我有:

As per the instructions, in my controller I have:

//PrintController.php
$data = array('name'=>'John Smith', 'date'=>'1/29/15');

$pdf = PDF::loadView('contract', $data);
return $pdf->stream('temp.pdf');

在我看来:

//contract.php
...
<p><?php echo $data->name ?><p>
<p>Signature</p>

但是当我尝试渲染页面时,出现错误:

But when I try to render the page, I get the error:

ErrorException (E_UNKNOWN) 
Undefined variable: data

我不确定为什么loadView()方法没有将$data变量传递给视图.在控制器和/或视图中设置它是否缺少我的步骤?

I'm not sure why the loadView() method is not passing the $data variable to the view. Is there a step I'm missing in setting it up in the controller and/or view?

推荐答案

您正在使用的loadView方法将在将数据传递到视图之前使用extract方法.此方法提取所有数组元素,并根据元素的键为其创建变量

The loadView method you are using is going to use the extract method before passing the data to the views. This method extracts all the array elements, and creates variables for them based on the key of the element

这意味着您的数组键将是您的变量名,即$ name而不是$ data-> name.这在laravel中是相当标准的,例如在使用Blade Views时.

This means that your array keys are going to be your variable names, ie $name and not $data->name. This is fairly standard in laravel, for example when using Blade Views.

http://php.net/manual/en/function.extract.php

这篇关于DOMPDF loadView()错误-未定义的变量:数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆