Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


Twitter plugin for CakePHP

CakePHP plugin to interact with your Twitter account (see the Twitter API for reference). For example, it allows you to post a new status on Twitter after a Model::save(), on create, update, or both.

1. Installation

To install, run the following command from your app/plugins folder:

git clone git://

The plugin includes the Twitter DataSource for CakePHP by Alex Ciobanu, added as a git submodule. You have to update it:

git submodule init
git submodule update

2. Setup

2.1. Twitter account credentials

Duplicate the following file:


and rename it to ‘twitter.php’. Now open it and replace the username and password with yours.

2.2. Attach to Model

The plugin includes a Behavior that can be attached to any Model. The only configuration needed is when do you want to post a status on Twitter : on create, on update, or both.

class Post extends AppModel
	var $actsAs = array('Twitter.Twitterable' => array(
		'on' => 'create'

In this example, everytime a Post is created, a new status will be posted on your Twitter account.

2.3. Create a Model::twitterStatus() method

The Twitterable Behavior will look for a twitterStatus() method in your model. This method just have to return the status you want to post. Keep in mind that a status on Twitter is limited to 140 characters.

We included a convenient method to format a status, twitterFormatStatus(). This method takes 3 arguments:

  • $message: required text of the status ;
  • $url: optionnal url, to the full post for example. Will be shortened (we choosed ;
  • $ending: optionnal ending string if the status is too long. Defaults to ‘…’.

Full example:

class Post extends AppModel
	var $actsAs = array('Twitter.Twitterable' => array(
		'on' => 'create'
	function twitterStatus()
		$title = $this->data['Post']['title'];
		$url   = Router::url(array('controller' => 'posts', 'action' => 'view', $this->id), true);
		return $this->twitterFormatStatus($title, $url, '...');

The status will be the post’s title (cut and ended by ‘…’ if too long), followed by a space and the url in a shortened format.

3. Other Twitter interactions

Since we are using the Twitter DataSource, we can access various other Twitter API methods. Please read the Bakery article about the Twitter DataSource to see what you can do.

Note that we don’t need the plugin’s Behavior to access the DataSource methods, but only the plugin’s Twitter model.

Example: we look for ‘cakephp’ on Twitter and display the results :

class SomethingsController extends AppController
	var $uses = array('Something', 'Twitter.Twitter');

	function index()
		$ds = ConnectionManager::getDataSource('twitter');
		$test = $ds->search('cakephp');
Something went wrong with that request. Please try again.