A Trackback is a way for bloggers to recognise articles from other bloggers while keeping the content on their own site. For example, I could like an article onLifehacker, and instead
of commenting on Lifehacker, adding more content on their site, I could post an article on my own blog and do a Trackback to the original article. Increasing the content on my own site but also
giving props back to the original post. This is also a way for an up and coming blog to get their link on a well-known blog or website and drive more traffic to it. Of course
most site admins have to accept Trackbacks before the link to the derived post will appear (usually in the comments section, if not in a specialised Trackback part of the original post).
This post will describe how to create a controller that will accept trackbacks, and post links on your own blog, in CakePHP.
In technical terms, a Trackback is simply a HTTP POST to a website which must contain at least only a URL, or also a title, excerpt and blog name (blog_name). The website must return XML with a response
error (either 0 or 1) and a message if needed.
Click here if you want to get all the details, or just download the source code below:
cakephp-trackback-controller-1.0.zip (<8kB)
CakePHP Trackback Controller GitHub project page
If you haven't had to deal with XML files in your Cake app, you will need to allow the XML extension to be parsed by adding the following line to app/config/rotues.php
Router::parseExtensions('xml');
Now in your controller add the RequestHandler component.
var $components = array('RequestHandler');
Now in your trackback controller function (I created a Trackback controller with one function, index) you'll need to set the RequestHandler respondAs to XML:
$this->RequestHandler->respondAs('xml');
Again if you don't have any XML pages in your app you'll have to setup an XML default layout. I set it up in app/views/layouts/xml/default.ctp
You'll need to include the XML header, made easy if you use the XMLHelper. To do this add 'Xml' to the $helpers var in the controller:
var $helpers = array('Xml');
Then in the XML default layout setup the $content_for_layout (as you would for an HTML layout) and the XML header:
<?= e($xml->header()) ?>
<?= $content_for_layout ?>
Then to make the controller use this layout instead of the default HTML layout you'll need to change it:
$this->layoutPath = 'xml';
Now we have the XML layout setup and the controller is now using it, next is to actually configure the function to check the incoming data and validate it, and then return the correctly formatted XML.
In my example code I save the data to a trackback table, however you can have it save directly as a comment to the post that is it regarding.
In CakePHP you can get the values of a POST request, with a controller that isn't using an actual form, using $this->params['form'].
To be technically correct you will need to check this array for the 'url' variable, returning an error 1 if there is not 'url' variable.
From here you can either save the Trackback to its own Trackback table as I do in my example, or you can validate the Trackback and then
save it as a Comment to the specified post. This part I'll leave upto you. However if you have any questions on how to do any of this, or you've found
any errors in my instructions leave me a comment below (even better write a post on your own blog and send me a Trackback)
