AKKAweb
AKKAweb

Reputation: 3807

One CakePHP library for multiple apps

I have developed several small websites in CakePHP and one medium news site. However, the situation I am in right now is different and require some toughful analysis before proceeding, which brings me to the CakePHP community.

I am working for a company which currently uses straight PHP code to handle several websites (5 - 10) and all of them use one global includes folder which has things like Db connection, etc. The problem with all this, is that we want to upgrade everything to the latest version of PHP (Currently using 5.1) and in the process, I am thinking about upgrading our whole backend using CakePHP.

First of all is this a good idea? I get excited when working on CakePHP projects and have never had any issues.

Secondly, I have read from Cake experts that we should not add anything to the core side of Cake. However, if I am to create files that need to be accessed by all the sites, where could those go?

Currently, all the sites are OOP, but Cake is MVC. Should the global OOP classes(used by all sites) be converted to controllers/models or Vendor classes and placed in the Cake Core?

In general, what is the best approach?

CakePHP multiple sites approach Image

Upvotes: 4

Views: 2515

Answers (1)

AD7six
AD7six

Reputation: 66227

You can do that

The layout of a standard cakephp install is:

root
    /app
        ..
        /Plugin
        /Vendor
        /webroot
    /lib
        /Cake
    /plugins
    /vendors

Everything inside the app directory is specific to one application. The root lib, plugins and vendors directors apply to all applications in the install. I.e. without modifying anything you can do this:

$ cd root
$ cp -r app newapp

resulting in:

root
    /app
        ..
        /Plugin
        /Vendor
        /webroot
    /newapp
        ..
        /Plugin
        /Vendor
        /webroot
    /lib
        /Cake
    /plugins
    /vendors

Any plugin or vendor that is in the plugins or vendors can be loaded, without doing anything out of the ordinary, in both applications e.g. with the following:

root
    /app
        ..
        /Plugin
        /Vendor
        /webroot
    /newapp
        ..
        /Plugin
        /Vendor
        /webroot
    /lib
        /Cake
    /plugins
        /DebugKit
    /vendors

Both applications have the option to load and use debug kit.

You can also simply follow the instructions in the book which does the same thing but separating the location of common code from the applications themselves.

Be aware of the limitations of sharing dependencies

The problem with managing multiple applications like that is that all applications are tied to the same version of cake, and the same version of all shared plugins/vendors. CakePHP is not an idle project, it is in constant development - as are, likely most plugins/vendors you choose to use. For plugins and vendors that's easy to solve, the application Plugin/Vendor folder is checked before the install-wide folders.

However, rather than have one core and n apps - it's a more robust idea to have n complete installs for n applications:

/root
    /myapp
        /app
            /Plugin
            /Vendor
            /webroot
        /lib
            /Cake
        /plugins
        /vendors
    /app2
        /app
            /Plugin
            /Vendor
            /webroot
        /lib
            /Cake
        /plugins
        /vendors
    etc.

That way a new application can for example use the current version of Cake, without affecting any existing applications at all.

Upvotes: 2

Related Questions