我在我的控制器下面的代码:CakePHP的将数据传递到元素
function index()
{
$posts = $this->set('posts', $this->Portfolio->find('all'));
if (isset($this->params['requested']))
{
return $posts;
}
else
{
$this->set('posts', $this->Portfolio->find('all'));
}
}
什么,我想要它做的是:a)显示的投资组合项目列表的指数例如/portfolio/
和b)显示元素内的投资组合项目列表,以便用户可以通过网站从我的边栏访问投资组合项目。
这里是我的边栏元素:
<?php $posts = $this->requestAction('portfolio/index'); ?>
<ul>
<?php foreach ($posts as $post): ?>
<li><?php echo $this->Html->link($post['Portfolio']['title'], array('action' => 'view', $post['Portfolio']['id']));?></li>
<?php endforeach; ?>
</ul>
,然后我在我的布局,这样称呼它:
<?php $this->element('portfolio-nav', array('posts' => $posts)); ?>
但是它提供了以下错误:
Notice (8): Undefined variable: posts [APP/controllers/portfolio_controller.php, line 16]
并没有显示侧边栏中的项目列表。
我很确定我在控制器中写的是垃圾,所以如果任何人都可以帮助我使它工作,那就太棒了。
谢谢
还没虽然工作。 – Cameron 2011-04-02 12:41:34
将定义$ posts的行放在元素之外。 – Dunhamzzz 2011-04-02 12:44:03
投资组合指数工作正常。但是侧边栏列表并不是! – Cameron 2011-04-02 12:46:27