CakePHP: One core, many apps

CakePHP allows you to use one set of core files while maintaining multiple applications. It only takes a few steps, but may not be completely straightforward the first time you try. In my example, I have my workspace in ~/dev. I would setup my project in ~/dev/client_name/project_name. I place my cake core files in separate folders for each version in ~/dev/lib/cakephp.

The basics

  1. Download and install the latest version of CakePHP in a good location.
    Examples:
    • Linux: /usr/lib/cakephp/cake_1.2.2.8120
    • Windows: C:\lib\cakephp\cake_1.2.2.8120
  2. Copy the contents of the app folder to your project root. This is typically your webserver root or a folder within.
  3. Edit the following constants in webroot/index.php:
    1. ROOT
      You’ll want to change it so the root is one level back (your project root)
      	if (!defined('ROOT')) {
      		define('ROOT', dirname(dirname(__FILE__)));
      	}
      
    2. APP_DIR
      This one just gets set to blank since your root is also your app folder
      	if (!defined('APP_DIR')) {
      		define('APP_DIR', '');
      	}
      
    3. CAKE_CORE_INCLUDE_PATH
      Set the path to your cake core files. This can either be relative (../../core) or absolute (/path/to/core)
      	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
      		define('CAKE_CORE_INCLUDE_PATH', '..'.DS.'..'.DS.'..'.DS.'lib'.DS.'cakephp'.DS.'cake_1.2.2.8120');
      	}
      

      or

      	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
      		define('CAKE_CORE_INCLUDE_PATH', DS.'usr'.DS.'lib'.DS.'cakephp'.DS.'cake_1.2.2.8120');
      	}
      

It may take a little work to get the paths right, but it is well worth it. If you try to access your project and you get PHP errors, it probably means something is wrong with the core path above. The next project you start, just copy the app folder again as above, and copy the same index.php into any new projects you start.

Different paths for dev and live
The above example only covers one server configuration. Some developers may be developing in Windows while their production server runs Linux. In that case you will need to account for each server. A simple if/else statement will take care of this. In my case, I have “localdev” as the hostname for my local development server. Here is what my CAKE_CORE_INCLUDE_PATH configuration looks like:

	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
		if ($_SERVER['SERVER_NAME'] == 'localdev') {
			define('CAKE_CORE_INCLUDE_PATH', '..'.DS.'..'.DS.'..'.DS.'lib'.DS.'cakephp'.DS.'cake_1.2.2.8120');
		} else {
			define('CAKE_CORE_INCLUDE_PATH', DS.'usr'.DS.'lib'.DS.'cakephp'.DS.'cake_1.2.2.8120');
		}
	}

Upgrading CakePHP
Upgrading CakePHP is pretty easy. Just install the latest version of CakePHP as described above. Then when you are ready to upgrade your app, change the core path again and start testing. If you run into issues with the latest version, it is really easy to switch back.

Comments

  1. Subhas says:

    Wouldn’t it be easier to just create a symbolic link to /usr/lib/cakephp? If your on linux that is.

    • ricog says:

      @Subhas Thank you for pointing that out. If the code only runs on Linux servers, symbolic links may be a better option. I haven’t tried it myself, but it may save a few steps.

    • kaklon says:

      Under unix, a symbolic link is enough, and it saves you the hassle of modifying index.php

  2. Anthony says:

    If you are using a later version of PHP > 5.3 you can use symlink now on Windows platforms as well. This may be the beginning of helper app to quickly roll out sites.

  3. nphp101 says:

    Two thumbs up! GREAT TUTORIAL!
    I will link this tutorial to my blog. :d

  4. Very good tutorial. Helped me setup a common core in one shot. I converted multiple existing apps to this format and all of them worked at the first go – no hiccups :) Thanks dude!

  5. One question though… in installations where cake and app reside together, the way to make cake ignore real folders (subdomain folders) is to include the following in your .htaccess that existing above the cake and app folders.

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^/shop/(.*)$
    RewriteCond %{REQUEST_URI} ^/blog/(.*)$
    RewriteRule ^.*$ - [L]

    Now that the contents of the app folder are out on webroot, does this code go into the .htaccess that’s present there?

  6. matsakaw says:

    @kaklon
    can you give an apache2-linux-newbie-friendly example of your symbolic link solution?

    @anyone willing to help
    i am to develop a second cakephp application but would like to just use the same cake core for both apps. i tried, but have accepted that i cannot do this on my own anymore. suggestions/corrections are most welcome. thanks.

    i only have one development machine which i can use to develop secondapp while retaining firstapp (for occassional new requirements, bug fixes and testing):
    /cake_1.2
    /cake_1.2/cake
    /cake_1.2/docs
    /cake_1.2/vendors
    /cake_1.2/firstapp
    /cake_1.2/firstapp/config
    /cake_1.2/firstapp/controllers
    /cake_1.2/firstapp/models
    /cake_1.2/firstapp/views
    /cake_1.2/firstapp/webroot
    /cake_1.2/index.php
    /cake_1.2/.htaccess

    RewriteEngine on
    RewriteRule ^$ firstapp/webroot/ [L]
    RewriteRule (.*) firstapp/webroot/$1 [L]

    planned: to add
    /cake_1.2/secondapp
    /cake_1.2/secondapp/config
    /cake_1.2/secondapp/controllers
    /cake_1.2/secondapp/models
    /cake_1.2/secondapp/views
    /cake_1.2/secondapp/webroot

    what should .htaccess now be so that both apps are available?

    RewriteEngine on
    RewriteRule ^$ ???/webroot/ [L]
    RewriteRule (.*) ???/webroot/$1 [L]

    /etc/apache2/httpd.conf
    DocumentRoot /cake_1.2/firstapp/webroot

    /etc/apache2/sites-enabled/000-default symlinks to

    /etc/apache2/sites-available/default
    NameVirtualHost *

    ServerAdmin webmaster@localhost

    ### DocumentRoot /var/www/
    ### based on http://xmodx.com/guides/install-apache-and-enable-mod_rewrite-ubuntu-linux/
    ### i changed to below :
    DocumentRoot /cake_1.2/firstapp/
    ### end of edit

    Options FollowSymLinks
    AllowOverride None

    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all

    ### based on http://xmodx.com/guides/install-apache-and-enable-mod_rewrite-ubuntu-linux/
    ### added :

    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all

    ### end of add

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
    ServerSignature On

    Alias /doc/ “/usr/share/doc/”

    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128

    again, thanks for corrections and suggestions.

  7. [...] What I would like to do is have just a core cake folder and then have ALL the sites pull from that one cake core so I don’t have the same stuff several times over. I have been reading some tutorials on the web: http://rickguyer.com/cakephp-one-core-many-apps/ [...]

  8. [...] What I would like to do is have just a core cake folder and then have ALL the sites pull from that one cake core so I don’t have the same stuff several times over. I have been reading some tutorials on the web: http://rickguyer.com/cakephp-one-core-many-apps/ [...]

  9. [...] What I would like to do is have just a core cake folder and then have ALL the sites pull from that one cake core so I don’t have the same stuff several times over. I have been reading some tutorials on the web: http://rickguyer.com/cakephp-one-core-many-apps/ [...]

Submit a Comment