Software and Technology Ramblings

January 11, 2010

Calling CakePHP Code from Other PHP Code

Filed under: CakePHP — Tags: , — Doug Hays @ 11:13 pm

In order to call CakePHP from non-CakePHP code, we will copy index.php from /app/webroot and put it into our non-CakePHP codebase as cakephp.php.

Then, we’ll create a function requestCakePHP that requires the cakephp.php file:

function requestCakePHP($url)

{

// Set the url parameter for cakephp
$_GET['url'] = $url;

require_once 'cakephp.php';

// Fire up CakePHP and buffer results
ob_start();
$Dispatcher= new Dispatcher ();
$Dispatcher->dispatch($url);

return ob_get_clean();

}
?>

Then, whenever we need to call for some Cake, we just call requestCake with the URL minus the hostname:


echo requestCakePHP("/webpages/view/2");

In the example above, I have a webpages controller, a view action and am viewing webpage #2.

Powered by WordPress