Contact Us

The ultimate CakePHP bootstrap technique

Posted on 15/8/06 by Felix Geisendörfer

Ok, I shouldn't use such a buzz-headline, but I was very happy today, when I discovered a new way to bootsrap CakePHP wihout having to render a page. This is especially useful when you try to embed CakePHP in existing php apps (drupal, wordpress, ..., ?), or when you try to write unit tests for highly coupled classes such as Controllers or Models.

The idea is very simple, and I felt sort of stupid that I didn't see it earlier. When you look in your app/webroot/index.php file there are the following lines at the end:

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

Their only purpose is to intialize an instance of the Dispatcher class and to request a Controller action for our $url. But what it also does, is to check weather or not the $_GET['url'] variable contains the value "favicon.ico" in which case *no dispatching* is happening. Now this gives us an easy way for including the webroot/index.php file without automatically rendering a page:

$_GET['url'] = 'favicon.ico';

Now we can easily create instances of single controllers, models, etc. or use the Dispatcher to render a page. The only thing we might need to do is call loadController(null) for loading the AppController (or others) and calling loadModels() to load our model files.

--Felix Geisendörfer aka the_undefined


You can skip to the end and add a comment.

[...] The first file you should check out is the one that actually get's called first when a CakePHP site is being requested. It's the /app/webroot/index.php file and it's main purpose is to define the most important path's of the folder layout, you can find some information about that in the manual. After that the $_GET['url'] value is used to invoke the Dispatcher class (/cake/dispatcher.php). There is also a little if statement checking if this value is 'favicon.ico' which can be used to bootstrap CakePHP without invoking a Dispatcher request, which is handy if you want to use CakePHP with an existing CMS like drupal or wordpress. The last thing happening in here is always a check if DEBUG is > 0, and if so, the execution time of this request is added as a hidden html comment to the source of your site. [...]

Max said on Dec 24, 2006:

Very Intresting Post.... What exactly does:

doing here ?

Felix Geisendörfer said on Dec 24, 2006:

Well this post assumes you include CakePHP from a directory that is not within the one of your CakePHP application. Therefor this just stands for the relative path you have to take to get to /app/webroot/index.php.

Max said on Dec 25, 2006:

Thanks for the explanation. Is it already being used in your current Drake project ?

Aldo  said on Jun 17, 2007:


I tried this:

$_GET['url'] = 'favicon.ico';
require_once 'sb/app/webroot/index.php';

$Dispatcher =& new Dispatcher();
$content = $Dispatcher->dispatch('/controller/action');

The problem is, that i get allways the cake startpage.
I'm using

Any idea how to solve this?


Felix Geisendörfer said on Jun 17, 2007:

Aldo: Sry, don't have time to test this right now but will let you know if I do at some point.

side effects of percocet said on Nov 14, 2007:

side effects of percocet...


fields said on Jun 13, 2008:

This is a really great article -- And is the KEY to embedding Cake 1.1 apps in php ..

Im really suprised no one else has expressed more thanks ..

Also you will need to comment out this line in the cake source code in app/webroot/index.php

echo "";

in the cake code otherwise you get http warnings etc ..

thanks for writing this up ..

fields said on Jun 13, 2008:

the code didnt appear

the line that says echo getMicrotime in the app/webroot/index.php

comment that out

LuoBang  said on Nov 07, 2008:

I did not use this method before.But i think this is not very important.
I want to know some deep message about bootstrap.

pankaj  said on May 15, 2009:

can you tell me do i need to write boot strap for every appliction.

Netemp  said on Aug 04, 2009:

Great Article, thanks for sharing.

I want to understand the bootstrapping in detail.. could you point me to some article or post?

Many Thanks in Advance

Dave  said on Oct 06, 2009:

Does this mean that the controller is loaded without App_Controller?

This is scary as most AUTH work is done in the app_controller before filter

has this been reported to the devel team?

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.