You seem to have a few of the concepts wrong here. I'll try to explain.
> foreach ($menu as $Menu):
This is where the error is happening. The problem is that, in the
controller action, you created a view variable named $Menu. But your
code here is looking for an array named $menu. You have it backwards.
Think of a foreach like this:
foreach($data as $item) means: for each element in $data array, create
a copy and name it $item for use within this block of code.
I generally name my view variables $data because the model name will
be a key inside, eg. $data['Menu']['whatever']. (Just don't mix it up
with $this->request->data!)
The next problem is that you're trying to create a menu that includes
a link to the code to create the menu:
> array('controller' => 'Menu', 'action' => 'loadmenu')
You mentioned that you're doing the blog tutorial so I presume that
your menu should be links to all blog posts. If that's the case, you
don't need a Menu model or MenusController. Instead, you'll want to
find('list') or find('all') from Post to get all of the titles and IDs
of each Post. With that you can create a basic menu. Here's a really
basic example that may help explain MVC, taken from my own code, but
simplified a bit.
layout:
echo $this->element('Posts/menu');
app/View/Elements/Posts/menu.ctp:
$archive = $this->requestAction('/posts/archive');
// die(debug($archive));
<div id="menu">
<ul>
<?php
foreach ($archive as $item)
{
// die(debug($item));
?>
<li>
<?php
echo $this->Html->link(
$item['Post']['title'],
array(
'controller' => 'Posts',
'action' => 'view',
'id' => $item['Post['id']
),
array('title' => $item['Post']['title'])
);
?>
<span class="DatePosted">
<?php
echo date('l, F j, Y', $item['Post']['created']);
?>
</span>
</li>
</ul>
</div>
Uncomment the die(debug()) lines separately to get an understanding of
how the array of data is returned, and how foreach works.
PostsController:
public function archive()
{
$archive = Cache::read('PostArchive');
if (empty($archive))
{
$archive = $this->Post->fetchArchive();
Cache::write('PostArchive', $archive);
}
return $archive;
}
Notice that you don't need to set() a view variable here but instead
return it. This is because we're calling this controller function with
requestAction().
Also, if caching is enabled, we don't need to call the model (and thus
the database) to get the archive.
Post model:
public function fetchArchive()
{
return $this->find(
'all',
array(
'fields' => array(
'Post.id',
'Post.created',
'Post.title'
),
'order' => array(
'Post.created' => 'DESC'
)
)
);
}
Once again, this is pretty basic. It can be improved upon but it
should get you on your way.
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
Thursday, December 6, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment