Integrating Serendipity Blog Framework with Codeigniter

s9y’s Serendipity blogging framework supports an embedded mode so that it can be inserted into your existing web site.  But what about a site using CodeIgniter’s MVC framework?

Embedding the Serendipity blog is explained fairly well here. We modify that method slightly so that the blog.php file that you create contains the following:

<?php
   ob_start();
   chdir(“../www/blog”);
   require(“index.php”);
   global $contents;
   $contents = ob_get_clean();
   chdir(“../”);

Next, the controller name matches the subdirectory that the blog is installed in. For example, a site which uses /blog would create a controller named Blog. This controller would contain the following code OUTSIDE of the the class definition (but within the file)

global $contents;
require_once(“[Path to your blog.php file]”);

Lastly, the serendipity contents can be accessed within your controller by accessing the $contents global:

    public function index() {
       global $contents;
       global $serendipity;
      

You will notice I also have a reference to the serendipity global variable. The serendipity global is used to pass information around within the serendipity blog framework and with the method above it is then completely accessible within your controller.  The result is an integration that works unexpectedly well…not just on the surface but within the PHP code, as well.