Alternative Installation Locations
The standard CakePHP installation as documented on installation is simply to extract the CakePHP archive to your webserver’s document root, modify the database.php configuration, then start working on your application code in cake/app/ . While this is fine, you may wish to separate the core cake libraries from your application (to have multiple applications sharing the same core), and even have your development code outside of your webserver’s document root.
To clarify, any reference to “webroot” means the folder whose default location is cake/app/webroot , it does not mean your webserver’s document root.
Application Folder Structure
There are three parts to a Cake application - [1] the core CakePHP libraries , [2] your application code (ie controllers, models, layouts and views), and [3] your application webroot files (ie images, javascript and css).
Each of these three parts can be located separately on your filesystem, and only the webroot [3] needs to be accessible via your webserver. While the default configuration has the webroot as a subfolder of the application, this is by no means required. The mod_rewrite rules located in the webroot/.htaccess pipe all of your queries through the webroot/index.php , so as long as the appropriate paths are set in this file the core cakephp libraries and your application code can be safely kept out of your webserver’s document root.
So, to test this, follow a simple tutorial (ie blog_tutorial_-_1 ) to get a basic cakephp application up and running. Then,
- Move the corephp cake libraries [1] to c:\libs\cake_rc5 (will contain cake folder, which in turn contains config, docs, libs, scripts folders).
- Move the application files [2] to c:\development\cake_blog (will contain config, controllers, models, vendors, views folders etc), except for the “webroot” folder.
- Move the application webroot folder [3] to somewhere inside your webserver document root, such as c:\wamp\www\cake_blog (will contain css, files, img, js folders)
- Now edit the webroot index.php ( c:\wamp\www\cake_blog\index.php ), and change the definitions for:
- “ROOT” ⇒ ‘c:\development’ (the parent folder path of your application code, no trailing slash)
- “APP_DIR” ⇒ ‘cake_blog’ (the folder name of your application)
- “CAKE_CORE_INCLUDE_PATH” ⇒ ‘c:\libs\cake_rc5’ (the folder containing the “cake” subfolder, with core CakePHP libraries)
It is recommended instead of using backslashes in the above path to use the “DS” definition. Using a foreslash (Windows XP, Apache 1.3.33, PHP 4.3.11) resulted in “Missing View” errors.
Partial contents of index.php:
/**
* These defines should only be edited if you have cake installed in
* a directory layout other than the way it is distributed.
* Each define has a commented line of code that explains what you would change.
*
*/
if (!defined('DS'))
{
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('ROOT'))
{
//define('ROOT', 'FULL PATH TO DIRECTORY WHERE APP DIRECTORY IS LOCATED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
define('ROOT', 'c:'.DS.'development' );
}
if (!defined('APP_DIR'))
{
//define('APP_DIR', 'DIRECTORY NAME OF APPLICATION';
define ('APP_DIR', 'cake_blog');
}
/**
* This only needs to be changed if the cake installed libs are located
* outside of the distributed directory structure.
*/
if (!defined('CAKE_CORE_INCLUDE_PATH'))
{
//define ('CAKE_CORE_INCLUDE_PATH', FULL PATH TO DIRECTORY WHERE CAKE CORE IS INSTALLED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
define('CAKE_CORE_INCLUDE_PATH', 'c:'.DS.'libs'.DS.'cake_rc5' );
}
///////////////////////////////
//DO NOT EDIT BELOW THIS LINE//
///////////////////////////////
Now you should be able to access your application via the cake_blog folder in your document root, ie via http://localhost/cake_blog/posts . If you get a blank view recheck the paths entered in the index.php, specifically that you have no trailing slashes on the paths, and the APP_DIR is just the folder name, not the entire path. Also check your PHP error log, as the missing file will be listed.
Redundant Files
In splitting this cake application up like this, there are a few files that are no longer needed. Specifically, the .htaccess and index.php files found in /cake and /cake/app are not used, as these two folders are no longer accessible via the webserver. These files simply redirected any calls to the /cake/app/webroot folder, and now this is the only access point anyway.
Webroot Via Apache Alias
In the above example, the application “webroot” folder (c:\wamp\www\cake_blog) is located inside the webserver’s document root. Occasionally you may wish for this webroot folder to be located outside of this document root (perhaps in your development folder), with an alias set to this location.
If you simply set up an alias in Apache and access your application via it, you will find that all requests give a “400 Bad Request - Invalid URI in request GET <your request url>”. This is due to the mod_rewrite rules of the webroot’s .htaccess being unaware of your Alias. The solution for this is to add a “RewriteBase” line to the .htaccess, with the same base as your Alias.
So, if you have set the alias:
Alias /cake_blog "C:\development\cake_blog\webroot"
You need to add:
RewriteBase /cake_blog
to the C:\development\cake_blog\webroot\.htaccess file . This file’s total contents should be:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cake_blog
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Now when you request your cake application through the alias you will see the result of query being executed, and the appropriate views and layouts being rendered. However, you will not see any of the “webroot” files (javascript, css, images etc). To fix this you need to define the “WEBROOT_DIR” in the webroot index.php file, to the name of your alias.
Partial contents of C:\development\cake_blog\webroot\index.php:
/**
* These defines should only be edited if you have cake installed in
* a directory layout other than the way it is distributed.
* Each define has a commented line of code that explains what you would change.
*
*/
if (!defined('DS'))
{
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('ROOT'))
{
//define('ROOT', 'FULL PATH TO DIRECTORY WHERE APP DIRECTORY IS LOCATED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
define('ROOT', 'c:'.DS.'development' );
}
if (!defined('APP_DIR'))
{
//define('APP_DIR', 'DIRECTORY NAME OF APPLICATION';
define ('APP_DIR', 'cake_blog');
}
/**
* This only needs to be changed if the cake installed libs are located
* outside of the distributed directory structure.
*/
if (!defined('CAKE_CORE_INCLUDE_PATH'))
{
//define ('CAKE_CORE_INCLUDE_PATH', FULL PATH TO DIRECTORY WHERE CAKE CORE IS INSTALLED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
define('CAKE_CORE_INCLUDE_PATH', 'c:'.DS.'libs'.DS.'cake_rc5' );
}
if (!defined('WEBROOT_DIR'))
{
define ('WEBROOT_DIR', 'cake_blog' );
}
///////////////////////////////
//DO NOT EDIT BELOW THIS LINE//
///////////////////////////////
