Integrating Wikka and CakePHP

This page has been deprecated.
This page covers CakePHP 1.1 and is not applicable to later versions. Please visit the updated WikkaCase page for covereage of CakePHP 1.3.

See also:
Wikka Developer Blog
The Bakery (archived version)

Why use both?

I advocate the use of Wikka as a presentation framework for rapid development of Internet-based applications. Wikka does a great job of handling user authentication, session tracking, and presentation, but does not offer much in the way of easily customizable, high-level database read/write access. Enter CakePHP: A rapid-development PHP framework that handles many of the low-level DB access details so you don't have to. While Cake itself offers similar framework functionality as Wikka, my goal was to implement a totally self-contained Cake application within Wikka. The look-and-feel would be all Wikka, while the Cake engine would provide a database abstraction layer independent of Wikka.

In addition, I'm a big fan of the MVC (model-view-controller) architecture, as it helps with cleanly delineating database functionality, business logic, and presentation. Cake is based upon the MVC (3-tier) model, while Wikka is better classified as a 2-tier model (database and business logic represented by one layer, presentation by the other). It's my belief that Cake could be used as a "plugin" framework that Wikka so badly needs. What follows is my proof of concept of one such implementation.


The goals for this project were simple:
I believe the first three goals have been met (and my three dogs are still happy with me, so I'm confident goal #4 was met as well).

Project platform

My development environment consists of Mac OSX 10.3.9, running the latest trunk version of Wikka. The Cake version I'm currently using is Please be aware that Cake versions starting with 1.2 have significant API changes that may not be compatible with the setup instructions that follow. I have not tested this setup on Wikka or earlier, but other than some directory restructuring, I see no reason why these instructions won't be applicable to earlier Wikka versions.

It's almost a certainty that mod_rewrite MUST be enabled AND operational. This is because Cake (much like Wikka) depends upon the structure of the URL to determine what action gets called. Since both Cake and Wikka (when mod_rewrite is enabled) depend on the same mechanism, there is an inherent conflict that must be resolved by some creative tinkering with the Cake dispatch mechanism. If that was just a bunch of Greek to you, trust me on this: You'll want mod_rewrite enabled!

Installing Cake

Download the latest 1.1.x version of Cake and use whatever method is appropriate for your system to unzip/untar the Cake distribution to your 3rdparty/plugins directory. As a matter of convenience, Unix/OSX users may want to either create a symlink to the cake distribution directory:

ln -s ./cake_1.1.16.5421 cake

or simply rename the directory:

mv cake_1.1.16.5421 cake

Windows users will have to opt for choice #2, as Windows does not support symlinks.

Cake is distributed with its own CSS stylesheet; many of the selectors conflict with the Wikka CSS selectors. As you will see shortly, there is a method that can be invoked in Wikka to include an external stylesheet in the <head> section that's generated by Wikka. I would recommend, at a minimum, that the Cake default CSS file (located at 3rdparty/plugins/cake/app/webroot/css/cake.generic.css) be copied to your Wikka css directory. Then, delete all sections but the section marked .

(If you decide you don't want to do this, there's a strong likelihood you will not see the error messages generated by the Cake built-in data validation methods!)

Setting up a Cake application as a Wikka action

Much of the agony involved with any customized Cake application involves the correct configuration of directories. Wikka itself is also very directory-oriented, so it's doubly important to get this right. There are probably any number of ways this can be accomplished, so my way is not necessarily the only or the best way.

The app directory under your 3rdparty/plugins/cake directory contains everything you need to get started. So, the easiest thing to do is simply copy the contents of the app directory into the new directory you've created for your action. For instance, if your new action will be called "caketest", then you'll first want to create that directory under actions. Then, copy the contents of 3rdparty/plugins/cake/app into this newly-created directory.

For Unix/OSX users:

mkdir actions/caketest
cp -pR 3rdparty/plugins/cake/app/* actions/caketest/

If you've done this correctly, there should not be a directory named app under actions/caketest! If there is, your action will not work correctly under Wikka.

Wikka expects a PHP file under actions/caketest with the name of caketest.php. Simply copy actions/caketest/index.php to actions/caketest/caketest.php.

The normal entry point for a Cake application is webroot/index.php. You'll notice that actions/caketest/index.php redirects to this webroot file. The webroot index.php file is where initial Cake configuration is accomplished, so setting this up correctly is critical.

Here are the steps you need to take. Other than creating your Cake application, this is probably the most complex process involved with Wikka/Cake integration. All of the following steps involve changes to webroot/index.php unless otherwise specified. I recommend making a copy of this file as a backup you can refer to if you get lost.

Locate the line beginning with define('ROOT'...), comment it out, and create a new line:

//define('ROOT', dirname(dirname(dirname(__FILE__))));
define('ROOT', dirname(dirname(__FILE__)));

Next, locate the line beginning with define('APP_DIR'...), comment out and modify:

define('APP_DIR', '.');

Comment out/modify CAKE_CORE_INCLUDE_PATH:

define('CAKE_CORE_INCLUDE_PATH', dirname(dirname(dirname(dirname(__FILE__)))).DS.'3rdparty'.DS.'plugins'.DS.'cake');

Due to the fact that Cake is running as an embedded app within Wikka, the base URL that Cake creates doesn't take into account the extra directory levels in which the Cake app resides. Therefore, the BASE_URL parameter must be set explicitly. Locate the closing brace, }, immediately after the CAKE_CORE_INCLUDE_PATH line and add the following, replacing the URL with your Wikka's URL used to access the caketest action:

if (!defined('BASE_URL'))
	define('BASE_URL', 'http://alabaster.local/wikka-cake/CakeTest');

OK, now for a bit of chicanery. You should find a couple of lines towards the end of the file that look like this:

if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') {
} else {
	$Dispatcher=new Dispatcher();

This is the "heart" of the Cake application, as the Dispatcher is what kicks off the Cake core engine. The problem here is that there is a conflict between the way Cake parses a URL (it expects a controller action as the URL target) and what Wikka expects (an action name as the URL target). Thanks to the work of Felix Geisendorfer, we have an easy way of overriding the default Cake dispatch mechanism, allowing us to create a new Dispatcher and dispatch us to wherever we want to go. Since Wikka implements handlers as a URL extension (i.e., wikka/CakeTest/edit invokes the Wikka editor on the CakeTest page), we must use another mechanism to pass actions to Cake. I've chosen to use GET parameters (i.e., wikka/CakeTest?action=add). By default, no GET parameters should invoke your controller's index() method directly (don't worry if this doesn't make sense at the moment). Anything else passed as a GET parameter is a Cake action and should be dispatched as such.

Assuming your controller is named servers_controller.php, you will want your dispatch code to look like this (comment out the codeblock after require CORE_PATH . 'cake' . DS . 'bootstrap.php'; and add the following):

	$Dispatcher = new Dispatcher();
	$this->AddCustomHeader('<link rel="stylesheet" type="text/css" href="'.BASE_URL.DS.'..'.DS.'css'.DS.'cake.generic.css" />');
		$Dispatcher->dispatch('/servers/index', array('wakka'=>(object)$this));
	else if(true===isset($_GET['action']))
		$Dispatcher->dispatch('/servers/'.$_GET['action'], array('wakka'=>(object)$this));

This might not make much sense to you if you're learning Cake as you go. My suggestion would be to create a simple Cake application (the Cake tutorial is an excellent place to start), get it working as a standalone app, and then simply copy everything under your app directory to your caketest action.

Notice the AddCustomHeader call? This is a Wikka hook that allows you to inject additional <meta> or <link> tags into the Wikka-generated <head> section on the output page. If you modified and copied the cake.generic.css file using the instructions earlier, you will need to include this call in order to include the CSS file in your output. Otherwise, you can safely omit this line.

Database configuration

Ideally, your new embedded Cake app would use the DB access credentials in wikka.config.php. Since I've not figured out how to best do this in a secure manner, you'll need to configure your DB access credentials in the Cake-supplied config/database.php file (simply copy config/database.php.default to config/database.php and edit). This file should be self-explanatory; only the $default configuration needs to be modified.


It is possible to test your configuration without having actually implemented a Cake application. Remember the dispatch code you commented out earlier? Go back and uncomment this block, then comment out the dispatcher block you added. Create a new Wikka page and include your action (using the example in this tutorial, {{caketest}}). Then, access your page. If you're successful, you'll be greeted with the following:

Your database configuration file is present.

Cake is able to connect to the database.

along with some other information about Cake.


Additional resources

At this point, if you're ready to tackle a new Cake application, start with the extensive Cake documentation. The Cake tutorial is an excellent hands-on project that lends itself easily to conversion to a WikkaCake app. I've also found the ThinkingPHP blog to be an excellent source of information about Cake that's not covered by the Cake manual and API.

I will be posting a simple WikkaCake application I developed for the OpenNIC wiki to track OpenNIC public servers. It's based loosely upon the blog tutorial mentioned earlier.

Have fun!


There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki