如何在我的元素中调用 app_controller 中的函数

发布于 2024-10-08 00:09:47 字数 1306 浏览 1 评论 0原文

我正在开发一个网站;用户可以通过一个注册帐户添加汽车和手机。但是,我在右侧和左侧有最热门的框(汽车、手机),在中间我有最近添加的汽车和手机。我将热门点击分成元素,这些元素是 topCars、topMobiles。我正在考虑编写两个函数来完成我的工作。这两个功能应该是通用的,因为将来我们可能会添加一些其他服务,例如软件或电影。然而,第一个功能将获取最近的汽车,或手机......等,并将发布在我的主页中间。第二个功能应该获得热门(汽车、手机、电影......等)。我编写了以下代码:

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' => 3)); 
    $this->set($item,$top);
  }
}

我的代码对于最新添加的项目运行良好,但是当我尝试获得热门点击时,它会返回错误。 请帮助我知道如何让它发挥作用 谢谢

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' => 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

郁金香雨 2024-10-15 00:09:47
$this->requestAction('App/getTopData/'.$param);
$this->requestAction('App/getTopData/'.$param);
谎言月老 2024-10-15 00:09:47

我认为你不应该将这些方法放在 app_controller 中。您应该创建另一个控制器并将 getLatestData 和 getTopData 放在那里。然后从元素中使用 $this->requestAction 从视图调用控制器操作。当然,您还应该使用某种缓存来加快速度。

有些人会说你不应该使用 requestAction,但这里有一篇来自 Cake 开发人员的文章:
http://mark-story.com /posts/view/how-using-requestaction-increased-performance-on-my-site

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文