简体   繁体   English

CakePHP-自己的功能

[英]CakePHP - own functions

Long story short: I'm integrating the Facebook API (PHP-SDK) with CakePHP (I know that there are already existing integrations out there on GitHub but I use this exercise as a learning curve for CakePHP and MVC in general). 长话短说:我正在将Facebook API(PHP-SDK)与CakePHP集成在一起(我知道GitHub上已经存在现有的集成,但是我通常将此练习用作CakePHP和MVC的学习曲线)。 My question is the following: 我的问题如下:

I have a controller action that returns a list of friends, in a very simplistic format: 我有一个控制器操作,它以非常简单的格式返回朋友列表:

$data = array(
  'friends' => $this->_facebook->api('/me/friends')
);
$this->set($data);

$data is then being sent to the corresponding view where at the moment I'm simply calling print_r($friends); 然后,将$data发送到相应的视图,在这里我只是简单地调用print_r($friends); and that works like a charm. 就像魅力一样。 However, I'd like to order the friends array both ascending and descending, and before the MVC world, I was using this function: 但是,我想对friends数组进行升序和降序排序,在MVC世界之前,我正在使用以下函数:

public function listFriends($sort = "") {
        $sort = strtolower($sort);
        $friend_array = $this->_facebook->api('/me/friends');
        $friends = array();
        switch ($sort) {
            case "asc":
                foreach($friend_array['data'] as $friend) {
                    $friends[$friend['id']] = $friend['name'];
                }
                natcasesort($friends);
                return $friends;
                break;

            case "desc":
                foreach($friend_array['data'] as $friend) {
                    $friends[$friend['id']] = $friend['name'];
                }
                arsort($friends);
                return $friends;
                break;

            default:
                foreach($friend_array['data'] as $friend) {
                    $friends[$friend['id']] = $friend['name'];
                }
                return $friends;
        }
    }

My question is: how can I achieve the same in CakePHP, where can I place my 'custom' listFriends() function? 我的问题是:如何在CakePHP中实现相同的目标,在哪里可以放置“自定义” listFriends()函数? It's my understanding that if I place this into the controller, then someone can potentially access it by calling http://example.com/controller/listFriends , which I would obviously like to avoid. 据我了解,如果将其放入控制器中,则有人可以通过调用http://example.com/controller/listFriends来访问它,我显然想避免这种情况。 I also assume that the .ctp file is not the best place for this code to be placed, so where then? 我还假定.ctp文件不是放置此代码的最佳位置,那么呢?

Components is the answer! 组件就是答案!

Should anyone run into the same 'issue': 如果有人遇到相同的“问题”:

<?php
App::uses('Component', 'Controller');
class SortComponent extends Component {
    public function listFriends() {
        //blablablabla
    }
}
?>

And in your controller add the following: 在您的控制器中添加以下内容:

public $components = array('Sort');

and you can access your method by calling 您可以通过调用来访问您的方法

$this->Sort->listFriends()

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM