Question

I'd like to use Cakephp classes (models) from regular php code unrelated to cakephp.

I tried this but it didn't work:

include('/lib/Cake/Core/App.php');
App::uses('Business', 'Model');
$b = new Business();

The last line gives me an error, and I've also tried several workarounds with no success...

I'll be ever grateful for a good answer :)

EDIT: Here's the solution I found! (Thanks, @Burzum!):

I copied webroot/index.php to webroot/cake_setup.php and removed the last few lines so it will only include stuff and not actually execute anything:

App::uses('Dispatcher', 'Routing');

$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(
    new CakeRequest(),
    new CakeResponse()
);

Then, this worked from external php code:

if (!defined('DS')) {
    define('DS', DIRECTORY_SEPARATOR);
}

include_once(__DIR__ . DS . '..' . DS . 'v2' . DS . 'app' . DS . 'webroot' . DS . 'cake_setup.php');
App::uses('AppModel', 'Model');
App::uses('Business', 'Model');

$b = new Business();

Thanks a lot guys!

Was it helpful?

Solution

CakePHP is not made to be used piece by piece. If you're looking for a standalone ORM go for doctrine or propel.

You can keep trying it but you'll have to run the bootstrapping process of the framework (see what it does inside webroot/index.php) and register it's class loader and point it the the core include path.

Edit:

While looking at this again today, I think the proper solution here would be to expose the stuff your need from your CakePHP app through an API that the other non-Cake app can consume. REST is pretty easy with CakePHP you might want to look at that solution. You don't tight couple your other app on the CakePHP app then. You can even upgrade to 3.0 without having to touch the other non-Cake app then.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top