开发者

cakephp display data from another controller in pages

开发者 https://www.devze.com 2023-04-05 10:58 出处:网络
I need to display latest article on my apps home page. After reading a lot of questions on stackoverflow, I\'ve tried importing the ArticlesController and using requestAction with no success. Maybe th

I need to display latest article on my apps home page. After reading a lot of questions on stackoverflow, I've tried importing the ArticlesController and using requestAction with no success. Maybe there's a catch that I'm missing, since PagesController doesn't use a model.

What would be the best way to do this?

////////////// edit:

I've got the requestAction working, using following code

home.ctp view:

$lastarticle = $this->requestAction('/Articles/getarticle');

ArticlesController controller:

public function getarticle() {
    $lastarticle = $this->Article->find('first', array('order' => array开发者_StackOverflow('Article.id DESC')));
    return $lastarticle;
}

Now, I'd like to know if requestAction is an appropriate way of solving this problem because I've read that it's a waste of resources.


If your home page just displays articles, why not make your articles/index your home page (it doesn't have to be pages/display/home)? Look into cake routing.

Edit: You can use loadModel or requestAction (post your code for requestAction). Either way, you should cache it for better perfomance http://book.cakephp.org/view/1380/Caching-in-the-Controller

0

精彩评论

暂无评论...
验证码 换一张
取 消