The ultimate CakePHP bootstrap technique
Posted by Felix Geisendörfer, on Aug 15, 2006 - in PHP & CakePHP » Core & Hacking
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:
-
} else {
-
$Dispatcher=new Dispatcher();
-
$Dispatcher->dispatch($url);
-
}
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';
-
require_once('path_to_app_dir'.DIRECTORY_SEPARATOR.'webroot'.DIRECTORY_SEPARATOR.'index.php');
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
9 Comments
Very Intresting Post.... What exactly does:
require_once('path_to_app_dir'.DIRECTORY_SEPARATOR.'webroot'.DIRECTORY_SEPARATOR.'index.php');
doing here ?
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.
Thanks for the explanation. Is it already being used in your current Drake project ?
Hello
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 1.2.0.5146alpha
Any idea how to solve this?
Thanks!
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...
news...
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 ..
the code didnt appear
the line that says echo getMicrotime in the app/webroot/index.php
comment that out



[...] 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. [...]