私は CakePHP (2.2.x) を使用してブログを作成しています。ブログの一部はサイドバーです。最近の投稿、アーカイブ、メタを含むワードプレスのようなサイドバー。サイドバーに要素を使用するか、ヘルパーを使用するかがわかりません。
今のところ、要素を使用しています。いくつかのコード スニペット:
コントローラー/PostsController.php
public function index() {
$this->set('posts', $this->Post->find('all'));
}
public function show($id) {
$this->set('posts', $this->Post->find('all'));
$this->set('post', $this->Post->findById($id));
}
ビュー/投稿/index.ctp
<div class="span8">
<?php foreach ($posts as $post) { ... } ?>
</div>
<?php echo $this->element('sidebar', array('posts' => $posts)); ?>
表示/投稿/show.ctp
<div class="post">
... render the post using $post ...
</div>
<?php echo $this->element('sidebar', array('posts' => $posts)); ?>
ビュー/要素/sidebar.ctp
<?php foreach ($posts as $post) { ... render the recent posts ... } ?>
ご覧のとおり、要素show.ctp
とindex.ctp
インクルード要素の両方と両方に変数sidebar.ctp
が必要です。$posts
したがって、とアクション$this->Post->find('all')
の両方を呼び出す必要があります。一度だけ電話したいのですが、ヘルパーを使用するとうまくいくのではないかと考えています。index
show
$this->Post->find('all')