开发者

How to call a function in app_controller in my elements

开发者 https://www.devze.com 2023-01-30 09:35 出处:网络
I am developing a website; which the users can add cars and mobiles with an one registration account. However, I have on the right and on the left boxes for the top hits (cars,mobiles), and in the mid

I am developing a website; which the users can add cars and mobiles with an one registration account. However, I have on the right and on the left boxes for the top hits (cars,mobiles), and in the middle I have the recently added cars and mobiles. I split out the top hits into elements, and the elements are topCars, topMobiles.I was thinking to write two functions to do my stuff. Both functions should be generic, because in the future we might add some other services like software or movies. However The first function will get the recent cars, or mobiles... etc, and that will be published in the middle of my home page. The second function should get the top hits for (cars, mobiles, movies.... etc). I wrote the following code :

class AppController extends Controller
 {  
  var $uses = array('Car','Mobile');
  function Controler()
  {
   App::import('Core', 'Sanitize');
  }

  function beforeRender(){
    $this->getLatestData(); 
  }

  function beforeFilter() {
   $this->getTopData();
  }

  function getLatestData(){
   $latestCars = $this->Car->find('all', array('order' => array('id' => 'desc'),
                                                  'limit' => 7));
   $latestMobiles = $this->Mobile->find('all', array('order' => array('id' => 'desc'),
                                                  'limit' => 7));
   $this->set('cars',$latestCars);
   $this->set('mobiles',$latestMobiles);
  }


  function getTopData($item){ // to get the top hits

    $top = $this->$item->find('all', array('order' => array('hits' => 'asc'),
                                              'limit' =&开发者_开发问答gt; 3)); 
    $this->set($item,$top);
  }
}

My code works fine for the latest added items fine, but when I try to get the top hits it returns the error. Please help me to know how to make it works Thanks


$this->requestAction('App/getTopData/'.$param);


I think you shouldn't put these methods in the app_controller. You should make another controller and put getLatestData and getTopData there. Then from the element use a $this->requestAction to call the controller action from the view. Of course, you should also use some kind of caching to speed things up.

Some people will say you should not use requestAction but here is an article from one of the Cake developers: http://mark-story.com/posts/view/how-using-requestaction-increased-performance-on-my-site

0

精彩评论

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