Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following code in my controller:

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'));
    }
}

and what I want it to do is a) show a list of portfolio items for the index e.g. /portfolio/ and b) show a list of portfolio items inside an element so a user can access the portfolio items from my sidebar across the site.

Here is my element for the sidebar:

<?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>

and then I call it like so in my layout:

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

However it gives the following error:

Notice (8): Undefined variable: posts [APP/controllers/portfolio_controller.php, line 16]

And doesn't show the list of items in the sidebar.

I'm pretty sure what I have written in my controller is garbage, so if anyone can help me get it working, that'd be awesome.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
374 views
Welcome To Ask or Share your Answers For Others

1 Answer

I answered the very same question yesterday. Why is your controller action so complex? I guess you don't need anything more than

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... 
}

Then, in your index.ctp view:

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

If you want to be able to request this from every page in your site (sidebar or something), you can use requestAction, or place the $this->set... in your AppController. If you use requestAction in your element, you don't have to pass array('posts' => ...) in your $this->element call.


Ok, It's clear you need much more direction. Let me explain this step by step. First we need to create a beforeFilter on your AppController, so the $posts variable is accessible from everywhere in your application.

/app/app_controller.php:

<?php
class AppController extends Controller {
    function beforeFilter() {
        parent::beforeFilter();
        $this->set('posts', ClassRegistry::init('Portfolio')->find('all'));
    }
}

Next, we're creating a simple element in app/views/elements/foobar.ctp:

<?php debug($items); ?>

Lastly, we call the element from somewhere in a view:

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

We are assigning the $posts (which we have defined in your AppController) variable the items key, because our element expects a $items variable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...