Contributor: Evan Dudla
If CakePHP is your framework of choice, then this is the guide for you. After reading it, you should be familiar with:
- Getting CakePHP up and running on Pagoda Box
- Basking in the combined awesomeness of CakePHP and Pagoda Box
Getting the CakePHP Source
Downloading the Source from cakephp.org
To download the latest version of the CakePHP framework, visit cakephp.org and download the file archive.
Downloading the Source from Github
To download the latest version of the CakePHP framework from Github, git pull your copy from the official repository on GitHub.
If you obtain the source through git, it will be much easier to update the source while still maintaining and developing your application. To do this, initialize a new repository and add remote locations for both the CakePHP source and your repository on Github.
TerminalCreating a Local CakePHP Repo
To grab the CakePHP source, just git pull from the cake-update branch you just created. Then, git push your newly-downloaded source code to your own repository that you have created for your application.
TerminalPulling the CakePHP Source
The CakePHP Boxfile
Create a file named "Boxfile" in the base directory of your git repo and paste in the following code snippet. The Boxfile is your Pagoda Box config file. If you want the Boxfile explained in more detail, click here.
YAMLCakePHP Boxfile Example
This file defines your web-accessible directory (document_root), which directories are writable for caching and temporary files (shared_writable_dirs), and finally which PHP extensions are enabled for your application.
Basic CakePHP Environment Setup
In order to use CakePHP, it is highly recommended that you change the Security salt and seeds used by the application when hashing sensitive information, like passwords. To do this, open up app/config/core.php and modify the values for Security.salt and Security.cipherSeed with unique identifiers specific to your application.
As a convenience, there are utilities for generating the salt hash as well as the cipher seed.
PHPConfiguring Security.salt and Security.cipherSeed
In addition to Security settings, you must properly setup your application routing so that Cake can know where to send requests and how to interact within the framework. In the case of Pagoda Box, you can avoid the use of rewrites entirely and enable URL routing on a script level. To do this, open up app/config/core.php again and uncomment the line referencing App.baseURL like so:
PHPSetting Up Application Routing
Using APC to Handle Caching
When using CakePHP with Pagoda Box, rely on APC for caching your application.
Note!
Using APC to handle caching is optional, but it is recommended for the remainder of this guide.
To enable APC caching, you will need to modify the Boxfile configuration to enable the APC PHP extension in your application's environment. Because of Pagoda Box's modularity, you will only need to include the directive for the php_extensions in your custom Boxfile; all of the other settings will be inherited from the default CakePHP configuration!
YAMLAdding the APC PHP Extension to Your Boxfile
Once this is enabled, you need to tell CakePHP to use APC. Open up app/config/core.php and change the caching engine from File to Apc. This should be the very last line in an unmodified core.php file.
PHPSwitching the Caching Engine to APC
Connecting to a Database
You will almost always be using a database with your CakePHP installation. To setup a database, refer to the Creating a Database Guide.
Pagoda Box automatically generates environment variables for each of your databases and prepends the component name to the variables. Environment variables allow you to avoid hard-coding sensitive information into your application, keeping it from being visible in your GitHub repository. In addition, the use of environment variables helps keep your code environment independent—unreliant on any one specific database connection. To add or remove environment variables, visit the "Global Vars" tab of your dashboard.
You can access these environment variables from the PHP $_SERVER superglobal. To add this to the CakePHP configuration, copy database.php.default to database.php and add the following before the class DATABASE_CONFIG { ... } declaration:
PHPSetting Environment Server Variables in database.php
Put your newly-defined constants directly into the database configuration class. This includes adding a new key for the database socket that Pagoda Box requires. You need to use constants because of the PHP specification that requires a constant or literal as a value for a class's member variable (in this case, DATABASE_CONFIG::$default). Remember to change the driver to mysqli to accommodate the current configuration.
PHPChanging the Database Configuration Class to Use Environment Vars
As one last step, you may notice when committing your changes to git that your database.php file will not be present in your working changes. To fix this, remove the top line of .gitignore, specifically the one that reads /app/config. Commit both these changes to see that CakePHP can successfully connect to your database.
Table of Contents
Getting the CakePHP Source
- Downloading the Source from cakephp.org
- Downloading the Source from Github
- Creating a Local CakePHP Repo
- Pulling the CakePHP Source
The CakePHP Boxfile
Basic CakePHP Environment Setup
Using APC to Handle Caching
Connecting to a Database
- Setting Environment Server Variables in database.php
- Changing the Database Configuration Class to Use Environment Vars