Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在页面中显示来自其他控制器的数据?CakePHP4.x_Php_Cakephp_Cakephp 4.x - Fatal编程技术网

如何在页面中显示来自其他控制器的数据?CakePHP4.x

如何在页面中显示来自其他控制器的数据?CakePHP4.x,php,cakephp,cakephp-4.x,Php,Cakephp,Cakephp 4.x,我想将文章连接到页面,但在上一屏幕中出现错误 在您的主页操作中 公共功能主页(){ $this->loadModel('Articles'); $lastArticles=$this->Articles->find('all',['limit'=>3,'order'=>Articles.created DESC'])->toArray(); $this->set('lastArticles',$lastArticles); } View::set需要两个参数,一个是视图中变量的名称,第二个是

我想将文章连接到页面,但在上一屏幕中出现错误


在您的主页操作中

公共功能主页(){
$this->loadModel('Articles');
$lastArticles=$this->Articles->find('all',['limit'=>3,'order'=>Articles.created DESC'])->toArray();
$this->set('lastArticles',$lastArticles);
}

View::set
需要两个参数,一个是视图中变量的名称,第二个是视图中该变量的值。

您在PageController.php中,我从未将该控制器用于display()方法以外的其他用途。我不知道你所做的是否是预期用途

如果您想列出您的文章,我建议您在ArticlesController.php中设置它

//in src/Controller/ArticlesController.php
public function home() {
$lastArticles = $this->Articles->find('all', ['limit' => 3, 'order' => 'Articles.created DESC']);
  $this->set('lastArticles', $lastArticles);
} 
然后,您需要创建一个名为的视图模板,该模板与控制器中的方法名对应——对于我发布的方法,它是“home.php”

//在templates/Articles/home.php中
//.. 你的领域。。

您的模板文件名为
主页
,但控制器操作名为
主页
?不要将代码发布为屏幕提示
//in templates/Articles/home.php
<?php foreach ($lastArticles as $lastArticle): ?>
//.. your fields ..
 <?php endforeach; ?>