【问题标题】:Cakephp 2.0 + Elements + requestAction = empty resultCakephp 2.0 + 元素 + requestAction = 空结果
【发布时间】:2012-07-02 12:48:23
【问题描述】:

我正在尝试在 Cakephp 2.0 中使用 Elements,但没有运气。我有一个名为 Post 的模型、一个名为 Posts 的控制器和各种视图。在布局中,我想包括(对于每个页面/视图)一个带有最新消息的框(比如 2)。

所以我创建了元素 dok-posts.ctp

<?php $posts = $this->requestAction('posts/recentnews'); ?>
<?php foreach($posts as $post): ?>


<div class="Post">
....

在我的 PostsController 中,我添加了函数 recentnews()

public function recentnews(){
$posts =  $this->Post->find('all',array('order' => 'Post.created DESC','limit' => 2));
if ($this->request->is('requested')) {
    return $posts;
} else {
    $this->set('posts', $posts);
    }
}

在我的布局中,default.ctp我调用我的元素

<?php echo $this->element('dok-posts'); ?>

问题是我收到了这条消息

Invalid argument supplied for foreach() [APP\View\Elements\dok-posts.ctp, line 9]

dok-posts.php 中调试,就在 $this-&gt;requestAction 之后,给了我一个空行。 recentnews 函数似乎没有返回任何内容(在函数中进行调试会返回一个包含找到的帖子的数组)。谁能告诉我我做错了什么?

【问题讨论】:

  • 似乎还有一些简单的调试要做。它会返回一个字符串而不是 $posts 吗?在您的控制器中尝试调试或 2 以了解发生了什么。你甚至进入了 if 块的第一部分吗?
  • 谢谢。好像总是进入else分支,从来没有进入第一个分支。
  • mark-story.com/posts/view/… 我会避免 requestAction。如果你必须把它放在几个页面中,你可以将变量传递给 AppController 中的视图或创建一个组件.. 甚至使用 beforeRender(),你有很多选择。但是在视图中请求一个动作对我来说似乎不太 MVC =P
  • @pleasedontbelong 。你提出了一个非常有趣的问题。我是 cakephp 世界的菜鸟,所以请原谅我缺乏知识。元素是“外部”实体。我的意思是视图与控制器相关,但元素不是(至少不直接 - requestAction)。您写道,我可以从控制器传递一个变量。如何在控制器中设置变量(比如说在函数 index 中),然后在元素中“传递”它?可以举个简单的例子吗?
  • @pleasedontbelong - 示例:由最近 5 篇新闻文章填充的元素。在您的示例中,您必须重复代码以在您想在其中使用该元素的每个控制器操作中检索这些文章(即使它是单行)。通过请求操作,您实际上只是将元素放入其中,瞧——它起作用了。海事组织完全值得。 (我想每个人都有)

标签: cakephp


【解决方案1】:

既然你发现实际上调用了这个动作,

$posts = $this->requestAction('posts/recentnews');

工作正常。在这里,为了清晰和扩展配置选项(用于以后对代码的更改),我建议您使用路由器数组而不是 URL

$posts = $this -> requestAction(array(
 'controller' => 'posts',
 'action' => 'recentnews'
));

现在到您的实际问题... 既然你说了,就一直进入else分支,

$this->request->is('requested')

可能无法按预期工作。试试这个(它非常适合我):

if (!empty($this -> request -> params['requested'])) {
   return $posts;
}

【讨论】:

  • 很酷,我可以帮忙。如果这是可行的解决方案,请通过勾选投票计数器下方的透明符号来“接受”它,这样它就会向上移动并显示给所有正在寻找相同问题的解决方案的人。
【解决方案2】:

在您的应用控制器中创建 beforefilter 和 getNewsElement 函数。

public function beforeFilter() {
    parent::beforeFilter();
    $data = $this->getNewsElement();
    $this->set('posts',$data);
}

function getNewsElement(){
# Put Post model in uses
$posts =  $this->Post->find('all',array('order' => 'Post.created DESC','limit' => 2));
return $posts;
}

dok-posts.ctp
#Remove <?php $posts = $this->requestAction('posts/recentnews'); ?>
<?php foreach($posts as $post): ?>

<div class="Post">
....

In  PostsController
public function recentnews(){
$posts =  $this->getNewsElement();

    $this->set('posts', $posts);

}

这会解决你的问题!

【讨论】:

    【解决方案3】:

    试试

    <?php $posts = $this->requestAction('/posts/recentnews'); ?>
    

    (注意前面的斜杠)

    【讨论】:

    • 谢谢,但我一直得到相同的结果。
    【解决方案4】:

    我已按照 Cakephp 指南中的示例进行操作。从它没有通过if语句的那一刻起,似乎控件有问题。

    $this->request->is('requested')
    

    所以我删除了

    is->('requested') 
    

    添加

    ->params['requested']
    

    它有效。

    感谢大家的帮助(特别是 wnstnsmth 的解决方案)。

    【讨论】:

      【解决方案5】:
      Try this
      
       <?php $this->requestAction('/controllerName/functionName');?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多