I am coding a blog in CakePHP in order to get myself started with the framework. The thing is, I’ve got my website’s layout, in which I want to display the latest posts (as long as its not the index page) and a tag cloud in all cases. The thing is, I can’t just go having every controller pass a variable to my layout so that it can render it, so I need some better way to query the Posts model for my posts and my Tags model for the tag cloud, which would be the best way to go about it? (so as to avoid mixing too much of the logic with the view).
The relevant part of my view’s code is as follows:
<?php
if ($this->params['controller'] != 'posts' || $this->params['action'] != 'index') {
?>
<section class="box">
<header>
<h3>Latest posts</h3>
</header>
</section>
<?php
// display an unordered list with all the latest posts
}
?>
<section class="box">
<header>
<h3>Tag cloud</h3>
</header>
<!-- display another unordered list with the tags-->
</section>
I am using, by the way, CakePHP 2.0.3
You could use element along with RequestAction: like
//in your element you can do like: $latestPosts = $this->requestAction('controllername/functionname');Check documentation for more information