저는 CakePHP (2.2.x)를 사용하여 블로그를 만들었으며 블로그의 한 부분은 사이드 바입니다. 최근 게시물, 아카이브 및 메타 데이터가 포함 된 워드 프레스와 같은 사이드 바. 사이드 바에 요소 나 도우미를 사용해야하는지 잘 모르겠습니다.CakePHP : Element 또는 Helper
현재 요소를 사용 중입니다. 일부 코드 조각 :
컨트롤러/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
변수가 필요합니다. 따라서 index
및 show
작업 모두에서 $this->Post->find('all')
으로 전화해야합니다. $this->Post->find('all')
한 번만으로 전화하고 싶습니다. 헬퍼를 사용하면 도움이 될지 궁금합니다.
예, 적합합니다. 감사. –
requestAction이 느립니다. 최소한 v2.x의 경우에는 전체 요청 크기 여야합니다. – geoidesic