cakePHP:如何在一个cakePHP布局页面上组合两个或多个应用程序 View ?

标签 cakephp view controllers

使用 cakePHP,我的目标是将两个或多个 Controller 的索引 View 组合在一个布局页面中。

示例: 我有以下 Controller :新闻、事件、链接。 我想在一个布局页面中显示每个表的最后五个条目。 此外,当选择 View 中的链接之一时,它应该将用户带到该记录的相应 View 。

我已阅读 views 上的书籍部分但不明白如何将 View 放入元素中来实现这一点。

令我困惑的是如何将三个独立的 Controller / View 组合成一个布局?

谢谢

最佳答案

在新闻、事件和链接模型中创建方法来获取最后 5 条记录。然后在 Controller 中将模型包含在 Controller::uses 属性中,或者在操作中使用 ClassRegistry::init() 来访问模型,例如

function my_action() {
  $news = ClassRegistry::init('News')->getRecent();
  $events = ClassRegistry::init('Event')->getRecent();
  $links = ClassRegistry::init('Link')->getRecent();
  $this->set(compact('news', 'events', 'links'));
}

然后,您可以从任何 Controller 操作调用这些模型方法,从而使您的应用程序保持干燥。

在您的 my_action.ctp View 以及实际上许多其他 View 中,只需渲染元素,例如

// my_action.ctp
<?php
echo $this->element('recent_news');
echo $this->element('recent_events');
echo $this->element('recent_links');
?>

然后,您的元素可以迭代 $news (或其他)变量,显示项目以及各自 Controller 中“查看”操作的链接。

仅仅因为 Controller 与模型匹配,并不意味着您不能在其中使用其他模型。

关于cakePHP:如何在一个cakePHP布局页面上组合两个或多个应用程序 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1749655/

相关文章:

Android 进度条的圆半径动态变化

angularjs - 如何在不同模块中的两个 Controller 之间共享服务

php - CakePHP 可包含 : Loading too much relations?

php - 使用 Paypal IPN 时如何将送货地址发送到 Paypal

cakephp - Cake PHP 2 自定义表单->标签

两个 View 之间的快速转换需要很长时间

mysql - 获取 MySQL View 上一行的值

node.js - 从nodejsexpress登录cakephp

ios - 重用 View / View Controller ?

javascript - $scope.$watch 数组内的特定对象 - Angular JS Controller