3

我只是想知道是否有人可以让我开始在 CakePHP 框架上编写私人消息传递系统。我的目标是类似于 Facebook 收件箱系统。当然不必那么复杂!

我目前有一个 AUTH 系统,用户可以登录和注销。

4

1 回答 1

4

The simplest way would be to just create a messages database table with at the very least, five columns: id, sender_id, recipient_id, subject, body. You can then also add other columns you desire, such as created.

You can then set up your controller as follows:

<?php
class MessagesController extends AppController {

    public function inbox() {
        $messages = $this->Message->find('all', array(
            'conditions' => array(
                'recipient_id' => $this->Auth->user('id')
            )
        ));
    }

    public function outbox() {
        $messages = $this->Message->find('all', array(
            'conditions' => array(
                'sender_id' => $this->Auth->user('id')
            )
        ));
    }

    public function compose() {
        if ($this->request->is('post')) {
            $this->request->data['Message']['sender_id'] = $this->Auth->user('id');
            if ($this->Message->save($this->request->data)) {
                $this->Session->setFlash('Message successfully sent.');
                $this->redirect(array('action' => 'outbox'));
            }
        }
    }
}

Obviously you'll need to flesh this example out, and change anything that may not apply to your application. You'll also need to add in checks for if the user is friends with the person they're trying to message if you want that as well.

于 2012-09-17T13:25:23.437 回答