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

发布于 2024-08-11 02:42:49 字数 313 浏览 2 评论 0原文

使用 cakePHP,我的目标是将两个或多个控制器的索引视图组合在一个布局页面中。

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

我已通读有关 视图 的书籍部分,但不知道如何创建视图到一个元素中就可以实现这一点。

让我困惑的是如何将三个独立的控制器/视图组合成一个布局?

谢谢

Using cakePHP my goal is to combine the index view of two or more controllers in one layout page.

Example:
I have controllers for: news, events, links.
I want to show the last five entries from each table in one layout page.
Also, when one of the links from the views is selected it should take the user to the respective view for that record.

I have read through the books section on views but don't see how making a view into an element would accomplish this.

What confuses me is how to combine from three separate controller/views into one layout?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-黛色若梦 2024-08-18 02:42:49

在新闻、事件和链接模型中创建方法来获取最后 5 条记录。然后在您的控制器中,要么在 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'));
}

您可以从任何控制器操作中调用这些模型方法,从而保持您的应用程序干燥的。

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

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

您的元素可以迭代 $news (或其他)变量,显示项目及其各自控制器中“视图”操作的链接。

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

Create methods in your News, Event and Link models for fetching the last 5 records. Then in your controller either include the models in the Controller::uses property, or in the action use ClassRegistry::init() to get access to the model, e.g.

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

You can then call these model methods from any controller action, keeping your application DRY.

In your my_action.ctp view, and indeed many other views, just render the elements e.g.

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

Your elements can then just iterate over the $news (or whatever) variable displaying the items with links to the 'view' actions in their respective controllers.

Just because a controller matches a model, doesn't mean you can't use other models in it.

溺ぐ爱和你が 2024-08-18 02:42:49

首先我要说的是,视图和控制器不一定捆绑在一起——Cake 会隐式添加由文件层次结构/命名约定指定的视图,但情况不一定如此。因此,尝试将视图视为与控制器解耦(这是使用 MVC 架构的主要目的之一)。

假设您的三个视图(A、B、C)正是您想要的复制方式,请将它们放入一个元素中(这只是位于特殊 APP/views/elements/ 目录中的视图文件)。现在,您可以在布局或其他视图中使用它们,只需调用 $this->element( 'elementName', array( 'options' ) ) 即可。

基本上,只需将要显示的代码抽象为元素,然后将这些元素插入到所需的布局中即可。

First I would say that views and controllers are not necessarily tied together -- Cake will implicitly add the view specified by the file heirarchy / naming convention, but this doesn't necessarily have to be the case. So try to think of the views as decoupled from the controller (which is one of the main purposes for using the MVC architecture).

Assuming your three views (A,B,C) are exactly how you want them copied, put them into an element (which is just a view file located in the special APP/views/elements/ directory). Now you can use them in either layouts or other views, just by making a call to $this->element( 'elementName', array( 'options' ) ).

Basically, just abstract the code you want to display into elements, then insert those elements into the desired layouts.

温馨耳语 2024-08-18 02:42:49

您可以将控制器设置为使用 RequestHandler,然后使视图元素能够从应用程序中的单独控制器获取自己的数据。

这个链接比我解释得更好
http://bakery.cakephp.org/articles/view/ create-reusable-elements-with-requestaction

要记住的一件事是,您调用的控制器操作应该考虑缓存自己的数据,这样您就不会进行不必要的数据库查询。

You can set up your controller to use the RequestHandler and then make your view elements capable of fetching their own data from separate controllers in your application.

This link explains it better than I can
http://bakery.cakephp.org/articles/view/creating-reusable-elements-with-requestaction

One thing to keep in mind is that the controller actions you are calling should account for caching their own data so you don't do unnecessary database queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文