CakePHP is really easy to set-up but until you do it once you really don’t know this. The documentation and tutorial at the cakePHP site is really vagaue for how you do things outside of the simple Post Controller/Model example they have. I mean how do you configure the routes file for more than one controller, etc?
This then is Part 1 of a serires on how to use cakePHP and I’ll be discussing on how to configure a new set-up to the point of being ready to develop it into a working site. I’m leaving it to you to grab the version that you want to use from cakePHP and while your at it go ahead and grab the scriptaculous libraries as we’ll be setting those up as well. Make sure to leave a donation if you can to these fine people, hmm thinking about it maybe myself if you’ve enjoyed this help.
So with your files extracted let’s look at the structure and how to upload them to your server. We will be working within the app folder, whether dealing with the routes, database, or adding the scriptaculous library. Specifically take notice of the webroot folder, that is where your “public” files are going to go. That means javascript, css, images etc. So go ahead and extract the scriptaculous library and move all the javascript files into app/webroot/js. Don’t include the lib/ or src/ folders in scriptaculous just the files. It’s not a big deal if you choose to.
Locate the database.php.default file in the app/config/ directory and open it up for modification. Then save that as database.php and use that for the correct configurations. Here is where we are going to set the configuration for database connection. At this point you’ll need to have a database set-up or two if you can. For kicks let’s say I have a database called part_1 and that will be used in this example.
Assuming that MySql database is going to be used here is what my database.php is going to look like:
var $default = array(
’driver’ => ‘mysql’,
’persistent’ => false,
’host’ => ‘localhost’,
#’port’ => ‘YOUR_PORT’
’login’ => ‘YOUR_DATABASE_USERNAME’,
’password’ => ‘YOUR_DATABASE_PASSWORD’,
’database’ => ‘part_1′,
’prefix’ => ”
);
var $test = array(
’driver’ => ‘mysql’,
’persistent’ => false,
’host’ => ‘localhost’,
‘login’ => ‘YOUR_DATABASE_USERNAME’,
#’port’ => ‘YOUR_PORT’
’password’ => ‘YOUR_DATABASE_PASSWORD’,
’database’ => ‘part_1_test’,
’prefix’ => ”
);
You see that I have two databases really part_1, and part_1_test. However using a single database I could just as easily set the prefix in the $test to ‘prefix’ => ‘test_’. I however do not like setting up test tables within actual data tables within a single database so it’s nice that cakePHP can split them out.
You’ll notice that I also included the ‘port’ => and have it commented out but you’re not seeing it in your database.php file. I included it to show that you can have a set-up for a not default MySQL configuration and this is how you do it. I’ve run databases that listen to ports 3307 or the socket files are in custom places so cakePHP would not be able to get connected without the port being set. Uncommenting the port and pointing in the right direction worked fine.
The next step is to set-up the config file located at app/config/core.php. Here we will be making some changes to the default for use in our application and to alert you to some functional changes of cakePHP.
The first item of interest is around line 85 or search for CAKE_SESSION_SAVE. By default cakePHP saves the sessions as defaulted in the servers php.ini. Generally this means saving it as a file somewhere on the server and for shared hosting this is bad. In general saving sessions to a file anywhere other users can access them is a bad idea. Even though one of the settings “cake” means saving the fiels to the app/tmp directory it’s still best to store the session information in the database. So change the CAKE_SESSION_SAVE to database.
With that done you’ll need to change the CAKE_SESSION_TABLE to the table name in the database you want to use. I leave mine at the default cake_sessions. However now you need to create the sessions table in the database. In the app/config/sql/sessions.sql file you’ll find the sql to create the table. So however you choose run that now.
Find CAKE_SESSION_STRING and change that to something other than what is printed. This string is used to create session id’s so if you leave it as is not only will cakePHP complain but you potentially have the same session id’s as every other cakePHP user. Changing as many characters will make your site more unique session wise.
I also like to set the CAKE_SESSION_COOKIE to something more descriptive of my application. So here I’ll be changing it to part_1_tutorial. Your choice really.
Unless you really don’t need a site administration area then don’t find CAKE_ADMIN and uncomment it. Otherwise do find it and uncomment it. A final note in the core.php file is the WEBSERVICES setting. As of cakePHP 1.2 this has been depreciated and cakePHP complains if you set to true. leave it as false.
Now you may upload the app/, cake/, docs, vendor/, .htaccess, index.php folders and files from the cakexxxx folder to your hosts web root folder, (generally public_html). Go ahead I’ll wait.
With all the files uploaded you should be able to point your browser to yourdomain.com and see the intro page. Hopefully the only problem you see is that the tmp/ folder may not have write permissions. To fix this you need to set the permissions on app/tmp/ folder to allow apache to write files in there. This is for the caching of cake pages. Once you get this done you should see that the databse is connected, tmp folder is writable and even session queries at the bottom of the page.
To get rid of the default cakePHP page simply create a page in app/views/pages/ called home.ctp. This is the default page if no controller is called in the uri.
So there you have it a quick and easy set-up of a cakePHP app. It probably took you longer to read this and upload the files than actually making it work and that is one benefit of cakePHP. I intentionally did not discuss routing as that is more of a topic on controller set-up.