CakePHP向元素传递数据

CakePHP向元素传递数据,php,cakephp,Php,Cakephp,我的控制器中有以下代码: function index() { $posts = $this->set('posts', $this->Portfolio->find('all')); if (isset($this->params['requested'])) { return $posts; } else { $this->set('posts', $this->Portfo

我的控制器中有以下代码:

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>
并且不会在侧边栏中显示项目列表

我很确定我在控制器中写的都是垃圾,所以如果有人能帮我让它工作,那就太棒了


感谢

元素方法中的第二个参数传递数据。您需要的是:

$this->element('portfolio-nav', array('posts' => $posts) );

请阅读。我昨天回答了同样的问题。为什么控制器的操作如此复杂?我想你只需要

function index() {
    $this->set('posts', $this->Portfolio->find('all'));
    // Make sure the model Portfolio is accessible from here, i.e. place this
    // action in PortfoliosController or load it using 
    // ClassRegistry::init('Portfolio')->find... 
}
然后,在index.ctp视图中:

<?php echo $this->element('foobar', array('posts' => $posts)); ?>
接下来,我们将在
app/views/elements/foobar.ctp
中创建一个简单的元素:

<?php debug($items); ?>

最后,我们从视图中的某个位置调用元素:

<?php echo $this->element('foobar', array('items' => $posts)); ?>


我们正在为$posts(我们在您的AppController中定义)变量分配
items
键,因为我们的元素需要一个
$items
变量。

您正在元素中设置$posts变量,并试图将其从布局传递到元素o.o

您的代码按此顺序运行

render layout
include element passing variable
set var with requestAction
弗雷奇

你应该做什么

render layout
include element (dont pass anything)
set var with requesAction (you get the data here so why are you trying to pass it in)

试试这个,让我知道它是否有效

我对Controller::index()函数做了一些更改:

function index () {
    // this is the problem
    // $posts = $this->set('posts', $this->Portfolio->find('all'));

    $posts = $this->Portfolio->find ( 'all' );

    if (isset($this->params['requested']))
    {
        return $posts;
    }
    else
    {
        // you already have the posts
        //$this->set('posts', $this->Portfolio->find('all'));

        $this->set ( 'posts', $posts );
    }
}
您的元素
组合导航没有更改

将布局中的调用更改为:

<?php $this->element ( 'portfolio-nav' ); ?>

我希望这对你有帮助。。
祝你发展顺利。

有一件事我认为没有人注意到,那就是你的错误消息:

注意(8):未定义变量:posts[APP/controllers/portfolio_controller.php,第16行]

在控制器中。您发布的内容看起来不错(虽然代码有点冗长),所以我想问一下PortfolioController中的第16行在哪里,为什么没有定义$posts


从控制器返回值以在元素中使用通常是有效的,因此没有问题。

这就是我非常简单地解决这个问题的方法:

控制器:

function birdsTwo() { 
    return $this->Bird->find('all', array('order' => 'Bird.name ASC', 'limit' => 2));   
}
<?php echo $this->element('quick_5_birds'); ?>
$birds = $this->requestAction('/birds/birdsTwo/');
    foreach($birds as $bird) {
          echo $this->Html->link(__($bird['Bird']['name'], true), 
array('controller' =>'bird','action' => 'view', $bird['Bird']['id'])) . "<br />";
}
布局:

function birdsTwo() { 
    return $this->Bird->find('all', array('order' => 'Bird.name ASC', 'limit' => 2));   
}
<?php echo $this->element('quick_5_birds'); ?>
$birds = $this->requestAction('/birds/birdsTwo/');
    foreach($birds as $bird) {
          echo $this->Html->link(__($bird['Bird']['name'], true), 
array('controller' =>'bird','action' => 'view', $bird['Bird']['id'])) . "<br />";
}

元素:

function birdsTwo() { 
    return $this->Bird->find('all', array('order' => 'Bird.name ASC', 'limit' => 2));   
}
<?php echo $this->element('quick_5_birds'); ?>
$birds = $this->requestAction('/birds/birdsTwo/');
    foreach($birds as $bird) {
          echo $this->Html->link(__($bird['Bird']['name'], true), 
array('controller' =>'bird','action' => 'view', $bird['Bird']['id'])) . "<br />";
}
$birds=$this->requestAction('/birds/birdsTwo/');
foreach($birds作为$bird){
echo$this->Html->link(uuu($bird['bird']['name'],true),
数组('controller'=>'bird','action'=>'view',$bird['bird']['id'])。“
”; }
您也可以这样做

class AppController extends Controller {

    public $var;    

    public function beforeFilter()
    {
         $this->var = ClassRegistry::init('Portfolio')->find('all');
    }
 }
class YourController extends AppController {

    public function index() {

        debug($this->var);
    }
}
查看Cakephp文档

如果在每个视图中都需要变量,则应在会话中设置它。

元素和参数

//Controller users
public function getUsers(){

     $users=  $this->User->find("all");

     return $users;
    }


//Element name users

$this->requestAction(array('controller'=>'users','action'=>'getUsers'))

//vista name view
 print_r( $this->element('users')); ?>
$this->element('elementname', array('var_name'=>$var));

将定义$posts的行放在元素之外。投资组合索引工作正常。但是侧边栏列表没有!如果我是诚实的,我正在努力弄清楚你为什么要使用请求操作。您不应该使用控制器为另一页调出内容。。把它放在你的布局/应用程序控制器中?这是正确的答案。按照这种方法,访问您传递的数据,例如:$posts.Worked对我来说非常好。这是我的观点,尽管我需要从每个页面请求它,但它不起作用,它只是说$posts变量未定义!照你说的做了,但是公文包上的元素是空的,当查看其他页面时,它会抛出一个错误,说帖子未定义。但是查看单个项目时不起作用:抱怨
注意(8):未定义索引:公文包[APP/views/elements/portfolio-nav.ctp,第3行]
有什么问题吗?这是第3行:
  • $post
    上进行调试,即
    调试($post)
    ,这样您就可以看到里面有什么了。您确定没有覆盖
    $post
    /
    $posts
    变量吗?这是非常基本的东西。我在哪里调试?这对你来说可能是基本的,但对我来说非常复杂。你的答案是冗长的重写代码并将其放入app_controller,我的答案是停止传递正在元素中初始化的未初始化变量…“如果你在元素中使用requestAction,你不必在$this->element调用中传递数组('posts'=>…)你能举个例子吗?