Cakephp 2.0+;元素+;requestAction=空结果

Cakephp 2.0+;元素+;requestAction=空结果,cakephp,Cakephp,我正在尝试使用CakePHP2.0中的元素,但没有运气。我有一个名为Post的模型、一个名为Posts的控制器和各种视图。在布局中,我希望(对于每个页面/视图)包含一个包含最新新闻的框(例如2) 因此我创建了元素dok posts.ctp <?php $posts = $this->requestAction('posts/recentnews'); ?> <?php foreach($posts as $post): ?> <div class="Pos

我正在尝试使用CakePHP2.0中的元素,但没有运气。我有一个名为Post的模型、一个名为Posts的控制器和各种视图。在布局中,我希望(对于每个页面/视图)包含一个包含最新新闻的框(例如2)

因此我创建了元素dok posts.ctp

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


<div class="Post">
....
在我的布局中,default.ctp我调用我的元素

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

试试看

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


(请注意前面的正斜杠)

因为您发现实际调用了该操作

$posts = $this->requestAction('posts/recentnews');
工作正常。在这里,为了清晰和扩展配置选项(用于以后对代码的更改),我建议您使用路由器阵列而不是URL

$posts = $this -> requestAction(array(
 'controller' => 'posts',
 'action' => 'recentnews'
));
现在谈谈你的实际问题。。。 既然你说了,它总是进入else分支

$this->request->is('requested')
可能无法按预期工作。试试这个(它非常适合我):


我遵循了Cakephp指南中的示例。从它没有通过if语句的那一刻起,似乎控件有问题

$this->request->is('requested')
因此,我删除了

is->('requested') 
添加

->params['requested']
它是有效的

感谢大家的帮助(尤其是解决方案)。

试试这个

在应用程序控制器中创建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);

}
公共函数beforeFilter(){
父::beforeFilter();
$data=$this->getNewsElement();
$this->set('posts',$data);
}
函数getNewsElement(){
#使用Post模型
$posts=$this->Post->find('all',array('order'=>'Post.created DESC','limit'=>2));
返回$员额;
}
dok-posts.ctp
#除去
....
后置控制器
公共职能新闻{
$posts=$this->getNewsElement();
$this->set('posts',$posts);
}

这会解决你的问题

似乎还有一些简单的调试要做。它会返回字符串而不是$posts吗?在控制器中尝试调试或2,以了解发生了什么。你是不是已经进入了if街区的第一部分了?谢谢。它似乎总是在其他分支中运行,而从不在第一个分支中运行。我会避免采取行动。如果必须将其放在多个页面中,可以将变量传递给AppController中的视图或创建组件。。或者甚至使用beforeRender(),您有许多选项。但是在视图中请求一个操作对我来说并不太MVC=P@pleasedontbelong . 你提出了一个非常有趣的问题。我是cakephp世界的新手,所以请原谅我知识的缺乏。元素是一个“外部”实体。我的意思是一个视图与一个控制器相关,但一个元素不相关(至少不是直接的-requestAction)。你写过我可以从控制器传递一个变量。如何在控制器中设置变量(比如在函数索引中),然后在元素中“传递”它?你能给我一个简单的例子吗?@PleaseDontAlley-example:一个由最近5篇新闻文章填充的元素。在您的示例中,您必须重复代码以在每个控制器操作中检索那些您希望在其中使用该元素的项目(即使它是一个单行程序)。通过一个请求操作,您实际上只是将元素放入其中,瞧,它工作了。我觉得这完全值得。(我想对每个人都是这样)谢谢你,但我一直得到同样的结果。很酷,我可以帮忙。如果这是有效的解决方案,请通过勾选投票计数器下方的透明符号来“接受”它,这样它将向上移动,并显示给正在寻找相同问题解决方案的所有人。
is->('requested') 
->params['requested']
Try this

 <?php $this->requestAction('/controllerName/functionName');?>
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);

}