Cakephp requestAction用法

requestAction用法

用法:

1.建立一个控制器函数用与返回数据


// Controller/CommentsController.php 
class CommentsController extends AppController { 
    public function latest() { 
      if (empty($this->request->params['requested'])) { 
           throw new ForbiddenException();
       } 
     return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10)); 
    }
 }
2.建立一个简单的元素调用这个函数


// View/Elements/latest_comments.ctp 
   $comments = $this->requestAction('/comments/latest'); 
   foreach ($comments as $comment) { 
       echo $comment['Comment']['title'];
   }

3.在任何位置放置元素以获取所需的输出

echo $this->element('latest_comments');

最好是用元素缓存不需要计算的数据。将元素的调用修改成如下的样子

echo $this->element('latest_comments', array('cache' => '+1 hour'));

你可能感兴趣的:(cakephp)