3

I am just wondering if anyone can get me started in writing a private messaging system on the CakePHP framework. I am aiming for something similar to the Facebook inbox system. Of course it doesnt have to be as complicated!

I currently have a AUTH system with users whom can log on and off.

user1436497
  • 437
  • 1
  • 6
  • 11
  • 1
    Why is everyone downvoting this..Its not like I am asking for code, I just wanna get pointed in the right direction.. – user1436497 Sep 18 '12 at 14:32

1 Answers1

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.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • I was thinking of putting all the functions into my User controller and have the User controller just use the messaging table. Which way would be more effective? – user1436497 Sep 17 '12 at 17:53
  • It's usually best practice to have a controller per model. As you are interacting with a `messages` database table, and therefore a `Message` model, you'd normally create a corresponding `MessagesController` class that contains methods to create, read, update and delete (CRUD) messages. You can then use the routing component in CakePHP to alter the routes if needs be. – Martin Bean Sep 17 '12 at 19:45