CakePHP:设置主页变量

发布于 2024-12-25 16:32:56 字数 1181 浏览 1 评论 0原文

将其添加到 app/controllers/pages_controller.php 内的 display 函数的末尾后,

    // grab all message lists
    $this->loadModel('MessageList');
    $this->set('messageLists', $this->MessageList->find('all'));

我尝试在 app/views/pages/home 中访问它。 ctp 之类的

<?php
        foreach($messageLists as $messageList) {
            print_r($messageList);
        }
    ?>

但我收到警告

Notice (8): Undefined variable: messageLists [APP\views\pages\home.ctp, line 9]

我的 app/config/routes.php 有:

/**
 * Here, we are connecting '/' (base path) to controller called 'Pages',
 * its action called 'display', and we pass a param to select the view file
 * to use (in this case, /app/views/pages/home.ctp)...
 */
    Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
 * ...and connect the rest of 'Pages' controller's urls.
 */
    Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

我需要更改什么才能访问我的 $messageLists主页?谢谢!

After adding this to the end of my display function inside app/controllers/pages_controller.php,

    // grab all message lists
    $this->loadModel('MessageList');
    $this->set('messageLists', $this->MessageList->find('all'));

I try to access it in app/views/pages/home.ctp like

<?php
        foreach($messageLists as $messageList) {
            print_r($messageList);
        }
    ?>

But I get the warning

Notice (8): Undefined variable: messageLists [APP\views\pages\home.ctp, line 9]

My app/config/routes.php has:

/**
 * Here, we are connecting '/' (base path) to controller called 'Pages',
 * its action called 'display', and we pass a param to select the view file
 * to use (in this case, /app/views/pages/home.ctp)...
 */
    Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
 * ...and connect the rest of 'Pages' controller's urls.
 */
    Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

What do I need to change so that I can access $messageLists in my home page? Thanks!

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

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

发布评论

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

评论(2

喜爱皱眉﹌ 2025-01-01 16:32:56

在你的display()方法的最后?好吧,这不起作用,因为它在设置视图变量之前调用 render() 并在此渲染页面。因此,将 set() 调用移到 render() 调用之前。

还有一个进一步的提示:您的实现做得不是很好,除非您想在每个页面上引发额外的查询,甚至不需要显示消息。

看起来您是 CakePHP 初学者,所以我建议您在手册中查找 requestAction() 以及视图中的元素如何工作。这将允许您使用任何视图文件中的元素和请求操作(例如 Messages/getListForUser)来显示消息列表。

At the very end of your display() method? Well this won't work because its calling render() before and by this render the page before you set the view vars for it. So move your set() call before the render() call.

Also a further hint: Your implementation is not very well done except you want to cause additional queries on each page that does not even need to display the messages.

It looks like you're a CakePHP beginner so I suggest you to lookup requestAction() in the manual and how elements in the views work. This would allow you to display the list of messages using an element in any view file by using an element and a request action to for example Messages/getListForUser.

谈场末日恋爱 2025-01-01 16:32:56

这应该有效。确保您在 $this->render() 调用之前设置变量。渲染调用停止控制器代码的执行并将视图渲染到浏览器。因此,只需将 this: 移动

// grab all message lists
    $this->loadModel('MessageList');
    $this->set('messageLists', $this->MessageList->find('all'));

到 $this->render() 调用之前,它就会起作用。

This should work. Make sure you're setting the variable before the $this->render() call. It is the render call that stops execution of the controller code and renders the view to the browser. So just move this:

// grab all message lists
    $this->loadModel('MessageList');
    $this->set('messageLists', $this->MessageList->find('all'));

to before the $this->render() call and it will work.

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