gpt4 book ai didi

cakePHP:通过 View 文件访问$this->request->data( Controller 传递的信息)

转载 作者:行者123 更新时间:2023-12-04 23:00:53 24 4
gpt4 key购买 nike

如何访问$this->request->data View 中的 Controller 传入?例如由 $this->set('sample') 定义的变量内部 Controller 可以通过 $sample 访问内部 View 和 .ctp 文件。如何访问存储在 $this->request->data 中的值通过查看文件?

最佳答案

CakePHP book$this->request在 Controller 、 View 和助手中可用。所以你可以使用 $this->request->data 访问它在你看来。如果你想给它一个更短的名字,你可以在你的 Controller 中设置它:

$this->set('requestData', $this->request->data);

如果你的 View 只需要几个变量,那么在你的 Controller 中解压请求数据并直接传递它们可能会更清晰。这也会更好 separation of concerns ;如果您稍后重构您的应用程序,只要您也传递这些参数,您就不必更新 View :
$this->set('name', $this->request->data('name'));
$this->set('age', $this->request->data('age'));

(请注意,我使用 CakePHP data() method 来访问这些属性;您不必将其视为数组)。

关于cakePHP:通过 View 文件访问$this->request->data( Controller 传递的信息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24851254/

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